首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
首页
学习
活动
专区
圈层
工具
MCP广场
MCP广场 >详情页
claude-code-mcp2025-05-216分享
github
基于Claude Code的MCP服务器,可以通过标准化的MCP接口使用Claude Code的强大的软件工程能力,包括代码生成与编辑、审查与分析、调试与故障排除等能力。
By auchenberg
2025-05-216
github
详情内容

Claude Code MCP

Claude Code MCP 是将 Claude Code 实现为 Model Context Protocol (MCP) 服务器的项目。该项目允许您通过标准化的 MCP 接口使用 Claude Code 强大的软件工程功能。

什么是 Claude Code?

Claude Code 是由 Anthropic 提供的用于软件工程任务的 CLI 工具,由 Claude 支持。它提供了一系列工具和功能,帮助开发者完成以下工作:

  • 代码生成和编辑
  • 代码审查和分析
  • 调试和故障排除
  • 文件系统操作
  • Shell 命令执行
  • 项目探索和理解

最初的实现是一个 JavaScript 模块,定义了与 Claude API 交互的提示和工具。

什么是 MCP?

Model Context Protocol (MCP) 是一种针对 AI 模型的标准化接口,它使得不同模型和提供商之间能够有一致的交互模式。MCP 定义了:

  • 工具:模型可以调用以执行操作的函数
  • 资源:模型可以访问的外部数据
  • 提示:预定义的对话模板

通过将 Claude Code 实现为 MCP 服务器,我们使其功能对任何兼容 MCP 的客户端可用,从而提高了互操作性和灵活性。

功能

  • 作为 MCP 服务器完全实现 Claude Code 的功能
  • 提供文件操作、Shell 命令和代码分析的工具
  • 暴露访问文件系统和环境信息的资源
  • 包含一般 CLI 交互和代码审查的提示
  • 与任何 MCP 客户端兼容
  • 使用 TypeScript 实现,并具有完整的类型安全

安装

# Clone the repository
git clone https://github.com/auchenberg/claude-code-mcp.git
cd claude-code-mcp

# Install dependencies
npm install

# Build the project
npm run build

使用

作为独立服务器运行

# Start the server
npm start

与 MCP 客户端一起使用

Claude Code MCP 可以与任何 MCP 客户端一起使用。这里是如何使用 MCP TypeScript SDK 连接到它的示例:

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";

const transport = new StdioClientTransport({
  command: "node",
  args: ["dist/index.js"]
});

const client = new Client(
  {
    name: "example-client",
    version: "1.0.0"
  },
  {
    capabilities: {
      prompts: {},
      resources: {},
      tools: {}
    }
  }
);

await client.connect(transport);

// Use Claude Code through MCP
const result = await client.callTool({
  name: "bash",
  arguments: {
    command: "ls -la"
  }
});

console.log(result);

可用工具

Claude Code MCP 提供以下工具:

  • bash:执行带有安全限制和超时选项的 Shell 命令
  • readFile:从文件系统中读取文件,并可设置行偏移量和限制
  • listFiles:列出带有详细元数据的文件和目录
  • searchGlob:搜索匹配 glob 模式的文件
  • grep:在文件中搜索文本,支持正则表达式模式
  • think:用于思考复杂问题的空操作工具
  • codeReview:分析和审查代码中的错误、安全问题和最佳实践
  • editFile:使用指定内容创建或编辑文件

工具详情

bash

{
  command: string;  // The shell command to execute
  timeout?: number; // Optional timeout in milliseconds (max 600000)
}

bash 工具包括防止执行潜在危险命令(如 curlwget 等)的安全限制。

readFile

{
  file_path: string; // The absolute path to the file to read
  offset?: number;   // The line number to start reading from
  limit?: number;    // The number of lines to read
}

searchGlob

{
  pattern: string;  // The glob pattern to match files against
  path?: string;    // The directory to search in (defaults to current working directory)
}

grep

{
  pattern: string;   // The regular expression pattern to search for
  path?: string;     // The directory to search in (defaults to current working directory)
  include?: string;  // File pattern to include in the search (e.g. "*.js", "*.{ts,tsx}")
}

可用资源

  • file: 访问文件内容 (file://{path})

    • 提供对文件内容的直接访问,并具有适当的错误处理
    • 返回指定文件的完整文本内容
  • directory: 列出目录内容 (dir://{path})

    • 返回一个包含文件信息对象的 JSON 数组
    • 每个对象包括名称、路径、是否为目录、大小和修改日期
  • environment: 获取系统环境信息 (env://info)

    • 返回关于系统环境的信息
    • 包括 Node.js 版本、npm 版本、操作系统信息和环境变量

可用提示

  • generalCLI: Claude Code 的通用 CLI 提示

    • 为 Claude 提供了一个全面的系统提示,使其可以作为 CLI 工具使用
    • 包括语气、风格、主动性及遵循惯例的指导方针
    • 自动包含环境详情
  • codeReview: 代码审查提示

    • 专用于代码审查任务的提示
    • 分析代码中的错误、安全漏洞、性能问题和最佳实践
  • prReview: 拉取请求审查提示

    • 专用于 PR 审查任务的提示
    • 分析 PR 更改并提供全面反馈
  • initCodebase: 初始化一个新的 CLAUDE.md 文件,包含代码库文档

    • 创建构建/检查/测试命令和代码风格指南的文档
    • 适用于使用 Claude Code 设置新项目

开发

# Run in development mode with auto-reload
npm run dev

架构

Claude Code MCP 是基于模块化架构构建的:

claude-code-mcp/
├── src/
│   ├── server/
│   │   ├── claude-code-server.ts  # Main server setup
│   │   ├── tools.ts               # Tool implementations
│   │   ├── prompts.ts             # Prompt definitions
│   │   └── resources.ts           # Resource implementations
│   ├── utils/
│   │   ├── bash.ts                # Shell command utilities
│   │   └── file.ts                # File system utilities
│   └── index.ts                   # Entry point
├── package.json
├── tsconfig.json
└── README.md

实现遵循以下关键原则:

  1. 模块化:每个组件(工具、提示、资源)都实现在单独的模块中
  2. 类型安全:所有组件都有完整的 TypeScript 类型定义
  3. 错误处理:所有操作都有全面的错误处理
  4. 安全性:对潜在危险的操作进行安全限制

实现细节

MCP 服务器设置

主服务器在 claude-code-server.ts 中设置:

export async function setupClaudeCodeServer(server: McpServer): Promise<void> {
  // Set up Claude Code tools
  setupTools(server);
  
  // Set up Claude Code prompts
  setupPrompts(server);
  
  // Set up Claude Code resources
  setupResources(server);
}

工具实现

工具使用 MCP SDK 的工具注册方法实现:

server.tool(
  "toolName",
  "Tool description",
  {
    // Zod schema for tool arguments
    param1: z.string().describe("Parameter description"),
    param2: z.number().optional().describe("Optional parameter description")
  },
  async ({ param1, param2 }) => {
    // Tool implementation
    return {
      content: [{ type: "text", text: "Result" }]
    };
  }
);

资源实现

资源使用 MCP SDK 的资源注册方法实现:

server.resource(
  "resourceName",
  new ResourceTemplate("resource://{variable}", { list: undefined }),
  async (uri, variables) => {
    // Resource implementation
    return {
      contents: [{
        uri: uri.href,
        text: "Resource content"
      }]
    };
  }
);

许可证

MIT

致谢

贡献

欢迎贡献!请随时提交 Pull Request。

免责声明

该项目与 Anthropic 无官方关联。Claude Code 是 Anthropic 的产品,而此项目是作为一个独立的 MCP 服务器实现 Claude Code。

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档