-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
94 lines (86 loc) · 2.7 KB
/
Copy pathworker.ts
File metadata and controls
94 lines (86 loc) · 2.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { createServer, registerAll } from "./server.js";
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
// Root — info page
if (request.method === "GET" && url.pathname === "/") {
return new Response(
JSON.stringify({
name: "italianparliament-mcp",
version: "0.27.0",
description:
"MCP server for querying Italian Parliament SPARQL endpoints (Camera + Senato)",
mcp_endpoint: "/mcp",
tools: 43,
}),
{
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
},
);
}
// Health check
if (request.method === "GET" && url.pathname === "/health") {
return new Response(
JSON.stringify({ status: "ok", runtime: "cloudflare-workers" }),
{
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
},
);
}
// CORS preflight
if (request.method === "OPTIONS" && url.pathname === "/mcp") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
// MCP endpoint
if (request.method === "POST" && url.pathname === "/mcp") {
try {
const server = createServer();
registerAll(server);
const transport = new WebStandardStreamableHTTPServerTransport({
sessionIdGenerator: undefined, // stateless
});
await server.connect(transport);
const response = await transport.handleRequest(request);
const headers = new Headers(response.headers);
headers.set("Access-Control-Allow-Origin", "*");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
} catch (err) {
return new Response(
JSON.stringify({
jsonrpc: "2.0",
error: {
code: -32603,
message: err instanceof Error ? err.message : String(err),
},
id: null,
}),
{
status: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
},
);
}
}
return new Response("Not Found", { status: 404 });
},
};