你需要 Node 22.18 或更高版本(可直接运行 TypeScript 文件)、Bun 或 Deno。
创建项目#
npx picomcp init my-server
cd my-server
npm install这会生成一个可以直接运行的服务器:
| 文件 | 用途 |
|---|---|
server.ts | 服务器定义:工具、资源、提示词 |
main.ts | 通过 stdio 启动服务器 |
server.test.ts | 在进程内调用工具的测试 |
想从零开始?安装这两个包,自己编写 server.ts:
npm install picomcp zod编写一个工具#
打开 server.ts:
import { pico } from "picomcp";
import { z } from "zod";
export const server = pico({ name: "my-server", version: "0.1.0" })
.tool("greet", {
description: "Greets someone by name.",
input: z.object({ name: z.string().describe("Who to greet") }),
run: ({ name }) => `Hello, ${name}!`,
});- 模型会读取
description来决定何时调用这个工具。请写给模型看。 input可以是任意 Standard Schema。在run中,name的类型是string。- 这里
run返回一个字符串。它也可以返回对象、内容块,或者抛出错误,详见工具。
测试#
import assert from "node:assert/strict";
import { test } from "node:test";
import { server } from "./server.ts";
test("greet says hello", async () => {
const result = await server.call("greet", { name: "Ada" });
assert.deepEqual(result.content, [{ type: "text", text: "Hello, Ada!" }]);
});npm testserver.call 会经过类型检查:工具名拼错或缺少参数都无法通过编译。
连接#
在 Claude Code 中:
claude mcp add my-server -- node /absolute/path/to/my-server/main.ts然后让 Claude 跟某人打个招呼。Claude Desktop、Cursor、VS Code 等客户端的配置见连接客户端。
调试#
MCP Inspector 可以手动调用工具,并查看原始消息:
npx @modelcontextprotocol/inspector node main.ts