-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathroute.ts
More file actions
70 lines (65 loc) · 1.84 KB
/
route.ts
File metadata and controls
70 lines (65 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
createMcpHandler,
withObservability,
type ObservabilityConfig,
} from "mcp-handler";
import { z } from "zod";
// Define the handler with proper parameter validation
const handler = createMcpHandler(
(server) => {
server.tool(
"echo-with-tracing",
"Echo a message back with observability tracing",
{
message: z.string().describe("The message to echo back"),
},
async ({ message }, extra) => {
// You can add custom span attributes within your handler
if (typeof req !== 'undefined' && req.traceId) {
console.log(`Processing echo request with trace ID: ${req.traceId}`);
}
return {
content: [
{
type: "text",
text: `Echo: ${message}`,
},
],
};
}
);
},
// Server capabilities
{
capabilities: {
tools: {},
},
},
// Route configuration
{
streamableHttpEndpoint: "/mcp",
sseEndpoint: "/sse",
sseMessageEndpoint: "/message",
basePath: "/api/mcp",
redisUrl: process.env.REDIS_URL,
}
);
// Observability configuration
const observabilityConfig: ObservabilityConfig = {
serviceName: "mcp-echo-service",
serviceVersion: "1.0.0",
traceIdHeader: "x-trace-id",
spanIdHeader: "x-span-id",
customAttributes: {
"service.environment": process.env.NODE_ENV || "development",
"service.instance": process.env.HOSTNAME || "local",
},
enableRequestLogging: true,
enableErrorTracking: true,
ignoreEndpoints: ["/health", "/metrics"],
samplingRate: 1.0, // 100% sampling for demo, adjust as needed
};
// Wrap the handler with observability
const observabilityHandler = withObservability(handler, observabilityConfig);
// Export the handler for both GET and POST methods
export { observabilityHandler as GET, observabilityHandler as POST };