Back to MCP directory
publicPublicdnsLocal runtime

bsmi021_mcp-file-context-server

一个为大型语言模型提供文件系统上下文的服务器,支持文件操作、代码分析和智能缓存等功能。

article

README

🚀 文件上下文服务器

文件上下文服务器是一个基于模型上下文协议(MCP)的服务器,专为大型语言模型(LLMs)提供文件系统上下文。它让LLMs能够读取、搜索和分析代码文件,还具备高级缓存和实时文件监控功能,为代码处理和分析提供了强大支持。

🚀 快速开始

使用以下命令安装文件上下文服务器:

npm install @modelcontextprotocol/file-context-server

启动服务器:

npx file-context-server

✨ 主要特性

文件操作

  • 能够读取文件和目录内容,支持多种文件编码。
  • 可列出文件及其详细元数据,还能递归遍历目录。
  • 具备实时文件监控功能,文件更改时可自动使缓存无效化。
  • 支持文件类型筛选。

代码分析

  • 可计算代码的环状复杂度,提取代码依赖项。
  • 能进行注释分析,检测重复行、长行(>100字符)、复杂函数。
  • 统计代码行数,包括总数、非空行和注释行。

智能缓存

  • 采用最近最少使用(LRU)缓存策略,文件更改时自动无效化缓存。
  • 缓存带有大小限制,支持配置上限,还能提供缓存统计和性能指标。
  • 缓存最近读取结果,优化搜索性能。

高级搜索

  • 支持正则表达式模式匹配,搜索结果可配置上下文行数。
  • 可进行文件类型过滤,支持多个模式搜索和排除模式。
  • 支持缓存结果搜索。

📦 安装指南

使用以下命令进行安装:

npm install @modelcontextprotocol/file-context-server

💻 使用示例

基础用法

列出目录中的文件及其详细元数据

{
  "path": "./src",
  "recursive": true,
  "includeHidden": false
}

读取文件或目录内容及元数据

{
  "path": "./src/index.ts",
  "encoding": "utf8",
  "maxSize": 1000000,
  "recursive": true,
  "fileTypes": ["ts", "js"]
}

在文件中搜索模式并获取上下文

{
  "pattern": "function.*",
  "path": "./src",
  "options": {
    "recursive": true,
    "contextLines": 2,
    "fileTypes": ["ts"]
  }
}

分析代码文件的质量指标

{
  "path": "./src",
  "recursive": true,
  "metrics": ["complexity", "dependencies", "quality"]
}

获取缓存统计信息和性能指标

{
  "detailed": true
}

📚 详细文档

错误处理

服务器提供详细的错误消息,包括具体的错误代码:

  • FILE_NOT_FOUND:文件或目录不存在
  • PERMISSION_DENIED:访问权限问题
  • INVALID_PATH:无效的文件路径格式
  • FILE_TOO_LARGE:文件过大
  • INTERNAL_ERROR:内部服务器错误

配置

通过以下环境变量进行配置:

MAX_CACHE_SIZE=1000  # 设置最大缓存条目数
CACHE_EXPIRATION=3600  # 设置缓存过期时间(秒)

开发

git clone [仓库地址]
cd 文件上下文服务器
npm install
npm start

📄 许可证

[此处填写许可证信息]

贡献指南

[此处填写贡献指南]

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client