代码工程MCP服务

通过标准化MCP接口提供代码生成、审查和调试等软件工程能力的服务实现

  • by auchenberg
  • JavaScript
  • 本地部署
  • 188 星标
  • 更新于
在 GitHub 查看 auchenberg/claude-code-mcp

使用说明

克劳德代码MCP

Claude Code MCP是 克劳德代码 作为 模型上下文协议(MCP) 服务器。该项目允许您通过标准化的MCP接口使用Claude Code强大的软件工程功能。

⚠️ 免责声明⚠️

Claude Code MCP是DevinAI自动生成的项目,DevinAI被提示分析Claude Code代码库并生成MCP服务器。

这是一个概念证明,我不建议任何人使用。

什么是克劳德密码?

Claude Code是Anthropic用于软件工程任务的CLI工具,由Claude提供支持。它提供了一组工具和功能,帮助开发人员:

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

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

什么是MCP?

模型上下文协议(MCP)是人工智能模型的标准化接口,可在不同模型和提供者之间实现一致的交互模式。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:使用行偏移和限制选项从文件系统读取文件
  • list文件:列出包含详细元数据的文件和目录
  • searchGlob:搜索与glob模式匹配的文件
  • 全局正则表达式打印:在支持正则表达式模式的文件中搜索文本
  • 思考:一个思考复杂问题的无操作工具
  • 代码审查:分析和审查代码中的错误、安全问题和最佳实践
  • editFile:创建或编辑具有指定内容的文件

工具详细信息

bash

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

bash工具包括安全限制,可以防止执行潜在危险的命令,如 curl, wget以及其他。

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)
}

全局正则表达式打印

{
  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://{path})

    • 通过适当的错误处理提供对文件内容的直接访问
    • 返回指定文件的全文内容
  • 目录:列出目录内容(dir://{path})

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

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

可用提示

  • 通用 CLI:Claude Code的通用CLI提示符

    • 为Claude提供全面的系统提示,使其充当CLI工具
    • 包括语气、风格、主动性和遵循惯例的指导方针
    • 自动包含环境详细信息
  • 代码审查:提示查看代码

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

    • PR审核任务的专门提示
    • 分析公关变化并提供全面的反馈
  • init代码库:使用代码库文档初始化新的CLAUDE.md文件

    • 为build/lint/test命令和代码样式指南创建文档
    • 可用于使用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"
      }]
    };
  }
);

许可证

麻省理工学院

致谢

贡献

欢迎投稿!请随时提交拉取请求。

免责声明

该项目与Anthropic没有正式关联。Claude Code是Anthropic的产品,这个项目是Claude Code作为MCP服务器的独立实现。