Ti serve Node 22.18 o successivo (esegue direttamente i file TypeScript), Bun o Deno.
Crea un progetto#
npx picomcp init my-server
cd my-server
npm installOttieni un server pronto all’uso:
| File | Scopo |
|---|---|
server.ts | La definizione del server: tool, risorse, prompt |
main.ts | Avvia il server su stdio |
server.test.ts | Un test che chiama un tool nel processo |
Preferisci partire da zero? Installa i due pacchetti e scrivi server.ts da solo:
npm install picomcp zodScrivi un tool#
Apri 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è ciò che il modello legge per decidere quando chiamare il tool. Scrivila per il modello.inputè qualsiasi Standard Schema. Dentrorun,nameè tipizzato comestring.- Qui
runrestituisce una stringa. Può anche restituire oggetti, blocchi di contenuto o lanciare un errore: vedi Tool.
Testalo#
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 è controllato dal sistema di tipi: un refuso nel nome del tool o un argomento mancante non compila.
Collegalo#
Per Claude Code:
claude mcp add my-server -- node /absolute/path/to/my-server/main.tsPoi chiedi a Claude di salutare qualcuno. Per Claude Desktop, Cursor, VS Code e altri, vedi Collegare un client.
Ispezionalo#
MCP Inspector ti permette di chiamare i tool a mano e vedere i messaggi grezzi:
npx @modelcontextprotocol/inspector node main.ts