English | 中文
A NestJS-based server implementing the Model Context Protocol (MCP). It exposes endpoints for MCP metadata, JSON-RPC tool invocation, prompts, resources, and Swagger docs.
- MCP metadata:
/.well-known/mcp.json - JSON-RPC tools:
POST /rpc - Prompts:
/prompts - Resources:
/resources - Swagger UI:
/docs
- Auto-register tools from
src/toolsby scanning*.tool.ts/*.tool.js.- Object style: default export conforms to
ToolDefinition(withname,handler, etc.) - Class style: export a class with
execute(params). - Tool naming priority (class-style): static
toolName> classname> filename base.
- Object style: default export conforms to
- Unified return type
Promise<unknown>; handler results are wrapped withPromise.resolve(...). - Global
ValidationPipeand DTO for input validation and type safety. - Swagger auto docs for quick inspection and testing.
pnpm install
pnpm run start:devThen visit:
- Swagger: http://localhost:3000/docs
- MCP metadata: http://localhost:3000/.well-known/mcp.json
- JSON-RPC: POST http://localhost:3000/rpc
Example calling an add tool (tool name may be add-numbers or the class static toolName):
curl -X POST http://localhost:3000/rpc \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "add-numbers",
"params": { "a": 1, "b": 2 },
"id": 1
}'If you set a class static name:
export class AddNumbersTool {
static toolName = 'add number';
execute(params: { a: number; b: number }) {
return params.a + params.b;
}
}Then call with method "add number".
Create a *.tool.ts under src/tools using either style:
- Object style:
export default {
name: 'echo',
handler: (params: unknown) => Promise.resolve(params),
};- Class style:
export class WeatherTool {
static toolName = 'weather';
execute(params: { city: string }) {
return { city: params.city, temperature: 26, condition: 'Sunny' };
}
}Tools are auto-registered and can be called via /rpc.
src/main.ts: app bootstrap, global pipes, Swaggersrc/app.module.ts: module wiringsrc/tools/: tools directory (auto register)src/rpc/: JSON-RPC controller & DTOsrc/mcp/: MCP metadata controllersrc/prompts/,src/resources/: prompts & resources APIs
- A global
AuthGuardis set asAPP_GUARD. Adjust it if you require authentication/authorization. - Ensure unique tool names to avoid registry overrides.
MIT