MCP 配置与服务器脚手架
通过 MCP 将 Claude 连接到各种工具的可复制粘贴起步模板。请裁剪到你需要的部分。
.mcp.json —— 声明服务器(项目共享)
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_TOKEN": "${GITHUB_TOKEN}" }
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "${DATABASE_URL}"]
}
}
}
:::warning 不要把密钥放进文件
引用环境变量(${GITHUB_TOKEN})—— 不要把令牌硬编码到一个会被提交的文件里。
:::
最小化 stdio 服务器(TypeScript)
一个暴露单个工具的微型服务器。请将处理函数调整为适配你的数据。
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "echo", version: "1.0.0" });
server.tool(
"echo",
{ text: z.string().describe("Text to echo back") },
async ({ text }) => ({ content: [{ type: "text", text: `You said: ${text}` }] }),
);
await server.connect(new StdioServerTransport());
最小化 stdio 服务器(Python)
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("echo")
@mcp.tool()
def echo(text: str) -> str:
"""Echo the text back."""
return f"You said: {text}"
if __name__ == "__main__":
mcp.run() # stdio transport