Claude Code MCP 是将 Claude Code 实现为 Model Context Protocol (MCP) 服务器的项目。该项目允许您通过标准化的 MCP 接口使用 Claude Code 强大的软件工程功能。
Claude Code 是由 Anthropic 提供的用于软件工程任务的 CLI 工具,由 Claude 支持。它提供了一系列工具和功能,帮助开发者完成以下工作:
最初的实现是一个 JavaScript 模块,定义了与 Claude API 交互的提示和工具。
Model Context Protocol (MCP) 是一种针对 AI 模型的标准化接口,它使得不同模型和提供商之间能够有一致的交互模式。MCP 定义了:
通过将 Claude Code 实现为 MCP 服务器,我们使其功能对任何兼容 MCP 的客户端可用,从而提高了互操作性和灵活性。
# 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

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 提供以下工具:
{
command: string; // The shell command to execute
timeout?: number; // Optional timeout in milliseconds (max 600000)
}

bash 工具包括防止执行潜在危险命令(如 curl
、wget
等)的安全限制。
{
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
}

{
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: 访问文件内容 (file://{path}
)
directory: 列出目录内容 (dir://{path}
)
environment: 获取系统环境信息 (env://info
)
generalCLI: Claude Code 的通用 CLI 提示
codeReview: 代码审查提示
prReview: 拉取请求审查提示
initCodebase: 初始化一个新的 CLAUDE.md 文件,包含代码库文档
# 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
实现遵循以下关键原则:
主服务器在 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。