Claude代码MCP服务器

通过模型上下文协议提供Claude代码执行和会话延续功能的服务器工具。

  • by pandysp
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 pandysp/claude-code-mcp

使用说明

克劳德代码MCP服务器

将Claude Code作为工具运行的MCP服务器,具有会话连续性。

通过提供两个工具 模型上下文协议:

  • claude_code --在任何提示下以一次性模式运行Claude Code
  • claude_code_reply --按线程ID继续之前的对话

这两个工具都解析来自CLI的结构化JSON输出,返回 session_id 用于穿线和 is_error 用于错误检测。30分钟的执行超时可防止进程失控。

快速开始

npx @pandysp/claude-code-mcp@latest

需要Node.js 20+和 Claude CLI 本地安装。

首次设置

在MCP服务器可以使用CLI权限之前,接受一次CLI权限:

npm install -g @anthropic-ai/claude-code
claude --dangerously-skip-permissions

配置

添加到MCP客户端配置中:

克劳德桌面/光标

{
  "mcpServers": {
    "claude-code-mcp": {
      "command": "npx",
      "args": ["-y", "@pandysp/claude-code-mcp@latest"]
    }
  }
}

帆板运动

{
  "mcpServers": {
    "claude-code-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@pandysp/claude-code-mcp@latest"]
    }
  }
}

OpenClaw(Docker)

在Docker容器内运行时(例如,作为OpenClaw MCP适配器服务器),使用容器的 node 二进制直接代替 npx:

{
  "command": "node",
  "args": ["/path/to/dist/server.js"]
}

工具参考

claude_code

使用一次性提示运行Claude Code。

参数 类型 必填 说明
prompt string yes 发送给Claude Code的提示
workFolder string no 工作目录的绝对路径

返回CLI结果文本。如果CLI输出JSON session_id,响应包括 structuredContent.threadId 用于 claude_code_reply.

claude_code_reply

继续之前的Claude Code对话。

参数 类型 必填 说明
threadId string yes 上次调用的会话ID
prompt string yes 后续提示
workFolder string no 应与原始调用的工作目录匹配

用途 --resume <threadId> 在引擎盖下保持对话上下文。

环境变量

变量 默认值 描述
CLAUDE_CLI_NAME claude 覆盖CLI二进制名称或绝对路径
MCP_CLAUDE_DEBUG false 启用stderr的详细调试日志记录

CLAUDE_CLI_NAME 支持简单名称(在PATH中查找)或绝对路径。相对路径被拒绝。

发展

npm install
npm run build
npm run test:unit    # 100 unit tests, fully mocked
npm run dev          # dev mode with tsx

归因

最初分叉自 @steipete/claude代码mcp.

许可证

麻省理工学院