使用说明
@mcpwatch/sdk
MCP服务器的观察性SDK。无需配置即可捕获工具调用、资源读取、提示和生命周期事件。
安装
npm install @mcpwatch/sdk
需要 @modelcontextprotocol/sdk >=1.0.0作为对等依赖。
快速开始
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { instrument } from "@mcpwatch/sdk";
const server = instrument(
new McpServer({ name: "my-server", version: "1.0.0" }),
{
apiKey: process.env.MCPWATCH_API_KEY!,
endpoint: "https://api.mcpwatch.dev",
}
);
// Define tools as usual — they're automatically instrumented
server.tool("get_weather", { city: { type: "string" } }, async (args) => {
return { content: [{ type: "text", text: `Weather in ${args.city}: 72°F` }] };
});
配置
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
apiKey |
string |
— | 必修的。 您的MCPWatch API密钥 |
endpoint |
string |
https://api.mcpwatch.dev |
摄入终点URL |
debug |
boolean |
false |
启用调试日志记录 |
sampleRate |
number |
1.0 |
事件采样率(0.0–1.0) |
maxBatchSize |
number |
50 |
每批最大事件数 |
flushInterval |
number |
1000 |
刷新间隔(毫秒) |
onQuotaWarning |
(info: QuotaInfo) => void |
-- | 配额接近限制时回调 |
关机
事件被批处理并异步发送。为确保在流程退出之前交付所有事件,请致电 server.close() 明确地:
// In your cleanup/shutdown handler
await server.close();
例子
看 示例/ 完整工作示例的目录。
需求
- Node.js 18+
@modelcontextprotocol/sdk>= 1.0.0