Skip to content

libra412/mcp-server

Repository files navigation

MCP Server with Self-Awareness

一个具有自我意识的MCP Server,支持文件操作、代码补丁、命令执行和LSP语义分析等功能。

功能特性

核心功能

  • 文件操作工具:读取、写入、列出、搜索文件
  • 代码补丁工具:应用和验证代码修改
  • 命令执行工具:运行命令和沙箱执行
  • LSP语义工具:符号提取、引用查找、签名获取、调用者查找

Agent状态机

  • 规划器(Planner):将复杂任务分解为可执行的步骤
  • 补丁器(Patcher):应用精确的代码修改
  • 验证器(Verifier):验证修改的正确性和完整性

工程上下文

  • 工作空间管理:根目录和忽略规则
  • 文件集管理:文件白名单和扩展名过滤
  • 依赖管理:解析Go、Node.js、Cargo、Python项目依赖

约束与安全

  • 文件修改限制:访问控制和文件大小限制
  • Diff限制:行数、添加、删除限制
  • 规则限制:安全规则检查(硬编码密钥、调试语句等)

安装

从源码构建

# 克隆仓库
git clone <repository-url>
cd mcp-server

# 构建服务器
go build -o mcp-server ./cmd/server

# 运行服务器
./mcp-server

使用配置文件

# 使用自定义配置
./mcp-server --config config.json

配置

默认配置

服务器使用以下默认配置:

{
  "server": {
    "host": "localhost",
    "port": 8080,
    "protocol": "http",
    "enableStdio": true,
    "enableHTTP": true
  },
  "agent": {
    "maxRetries": 3,
    "timeoutSeconds": 300,
    "enableSelfCheck": true
  },
  "guard": {
    "maxFileSize": 10485760,
    "maxPatchLines": 1000,
    "maxAdditions": 500,
    "maxDeletions": 500,
    "allowedPaths": [],
    "forbiddenPaths": [
      "/etc",
      "/usr",
      "/bin",
      "/sbin"
    ]
  },
  "tools": {
    "file": {
      "enabled": true
    },
    "patch": {
      "enabled": true,
      "maxPatchSize": 1000
    },
    "exec": {
      "enabled": true,
      "maxTimeout": 60,
      "enableSandbox": true
    },
    "lsp": {
      "enabled": true
    }
  },
  "prompts": {
    "plannerPath": "prompts/planner.md",
    "patcherPath": "prompts/patch.md",
    "verifierPath": "prompts/verify.md"
  }
}

自定义配置

创建 config.json 文件并修改配置项:

{
  "server": {
    "host": "0.0.0.0",
    "port": 9000
  },
  "guard": {
    "allowedPaths": [
      "/path/to/project",
      "/another/path"
    ]
  }
}

使用方法

HTTP模式

./mcp-server

服务器将在 http://localhost:8080 启动。

可用的HTTP端点:

  • POST /initialize - 初始化MCP连接
  • POST /tools/list - 列出所有可用工具
  • POST /tools/call - 调用工具

Stdio模式

./mcp-server --stdio

服务器将通过标准输入/输出进行通信。

可用工具

文件工具

  • file_read - 读取文件内容
  • file_write - 写入文件
  • file_list - 列出目录文件
  • file_search - 搜索文件内容

补丁工具

  • patch_apply - 应用代码补丁

执行工具

  • exec_run - 执行命令

LSP工具

  • lsp_symbols - 获取文件符号
  • lsp_references - 查找引用
  • lsp_signature - 获取函数签名
  • lsp_callers - 查找调用者

Agent工具

  • agent_process - 处理任务
  • agent_diagnose - 诊断Agent状态

自我检测

Agent具有自我检测能力,可以:

  1. 诊断状态:检查Agent是否处于错误状态
  2. 识别问题:识别初始化问题、配置问题等
  3. 请求升级:当发现能力不足时,请求必要的权限或功能升级

运行诊断:

# 通过HTTP
curl -X POST http://localhost:8080/tools/call \
  -H "Content-Type: application/json" \
  -d '{"name": "agent_diagnose", "arguments": {}}'

测试

运行测试套件:

# 运行所有测试
go test ./...

# 运行测试脚本
./scripts/run-tests.sh

# 格式化代码
./scripts/gofmt.sh

安全性

文件访问控制

  • 默认禁止访问系统目录(/etc, /usr, /bin, /sbin)
  • 支持配置允许的路径
  • 文件大小限制

代码修改限制

  • 最大补丁行数限制
  • 最大添加/删除行数限制
  • 禁止包含敏感信息的补丁

命令执行安全

  • 命令黑名单(rm -rf /等)
  • 沙箱执行模式
  • 超时控制

规则检查

  • 硬编码密钥检测
  • 调试语句检测
  • TODO注释检测

开发

项目结构

mcp-server/
├── cmd/server/          # 服务器入口
├── internal/
│   ├── protocol/        # MCP协议层
│   ├── tools/          # 工具实现
│   ├── agent/          # Agent状态机
│   ├── project/        # 工程上下文
│   ├── guard/          # 约束与安全
│   └── config/         # 配置管理
├── prompts/            # Prompt文件
└── scripts/           # 测试脚本

添加新工具

  1. internal/tools/ 下创建新包
  2. 实现工具函数
  3. cmd/server/main.go 中注册工具
  4. 添加测试

贡献

欢迎贡献!请:

  1. Fork项目
  2. 创建特性分支
  3. 提交更改
  4. 推送到分支
  5. 创建Pull Request

许可证

MIT License

联系方式

如有问题或建议,请提交Issue。

About

go mcp server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors