-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathsse.ts
199 lines (176 loc) · 6.03 KB
/
sse.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import express from "express"
import cors from "cors"
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"
import { serverFactory } from "./mcp-server.js"
import { fileURLToPath } from "url"
import { config } from "../lib/config.js"
// In a production environment, you'd handle sessions in Redis
// or another persistent store, rather than in memory
const transports = new Map<string, SSEServerTransport>()
// Helper function to create transport without setting headers
// (headers will be set by transport.start() when called by server.connect())
const setupSSEConnection = (res: express.Response, messagePath: string) => {
console.log("Creating SSE transport")
const transport = new SSEServerTransport(messagePath, res)
transports.set(transport.sessionId, transport)
return transport
}
export async function main(appName?: string, portOption?: string) {
const app = express()
// Log all requests for debugging
app.use((req, res, next) => {
console.log(
`Received ${req.method} request for ${req.url.replace(/\/[^/]+\//, "/[REDACTED]/")}`,
)
next()
})
app.use(cors()) // Enable CORS for all origins
app.use(express.json())
// Use provided port or fallback to env var or default from config
const port = portOption || process.env.PORT || config.serverPort
app.get("/", (req, res) => {
console.log("Health check")
res.send("Hello World")
})
// Define reusable handlers to avoid code duplication
const handleSSEConnection = async (
req: express.Request,
res: express.Response,
appName: string,
) => {
const messagePath = `/${req.params.external_user_id}/${appName}/messages`
const transport = setupSSEConnection(res, messagePath)
try {
console.log(
`Starting serverFactory for app: ${appName}, external_user_id: [REDACTED]`,
)
const server = await serverFactory({
app: appName,
externalUserId: req.params.external_user_id,
})
console.log("Server factory successful, connecting transport")
await server.connect(transport)
console.log("Connected to MCP server")
req.on("close", () => {
console.log("SSE connection closed")
transports.delete(transport.sessionId)
})
} catch (error) {
console.error("Error connecting to MCP server:", error)
if (error instanceof Error) {
console.error(error.stack)
res
.status(500)
.end(`Failed to establish SSE connection: ${error.message}`)
} else {
// For non-Error objects that were thrown
res
.status(500)
.end(`Failed to establish SSE connection: ${String(error)}`)
}
}
}
const handlePostMessage = async (
req: express.Request,
res: express.Response,
) => {
if (!req.query.sessionId || typeof req.query.sessionId !== "string") {
res.status(400).json({
error: "Invalid sessionId",
})
return
}
const transport = transports.get(req.query.sessionId)
if (!transport) {
console.log("No transport found")
res.status(500).json({
error: "No SSE connection established",
})
return
}
try {
await transport.handlePostMessage(req, res, req.body)
console.log("Message handled successfully")
return
} catch (error) {
console.error("Error handling message:", error)
if (error instanceof Error) {
console.error(error.stack)
res.status(500).json({
error: error.message,
})
} else {
// For non-Error objects that were thrown
res.status(500).json({
error: String(error),
})
}
return
}
}
if (appName) {
// If app is explicitly passed via CLI, set up specific routes for that app only
app.get(`/:external_user_id/${appName}`, async (req, res) => {
console.log(`GET /:external_user_id/${appName}`)
await handleSSEConnection(req, res, appName)
})
app.post(`/:external_user_id/${appName}/messages`, async (req, res) => {
console.log(
`POST /:external_user_id/${appName}/messages`,
"[REDACTED]", // Redacted sessionId
)
await handlePostMessage(req, res)
})
} else {
// Generic routes for any app
app.get("/:external_user_id/:app", async (req, res) => {
console.log(`GET /:external_user_id/[REDACTED] (app: ${req.params.app})`)
await handleSSEConnection(req, res, req.params.app)
})
app.post("/:external_user_id/:app/messages", async (req, res) => {
console.log("POST /:external_user_id/:app/messages", "[REDACTED]") // Redacted sessionId
await handlePostMessage(req, res)
})
}
app.on("error", (err) => {
console.error(err)
})
// Add a global error handler to catch unhandled promise rejections
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason)
})
app
.listen(port, () => {
console.log(`Server is running on port ${port}`)
console.log("Routes configured:")
console.log("- GET / - Health check")
if (appName) {
console.log(
`- GET /:external_user_id/${appName} - App-specific SSE connection endpoint`,
)
console.log(
`- POST /:external_user_id/${appName}/messages - App-specific message handler`,
)
} else {
console.log(
"- GET /:external_user_id/:app - App-specific SSE connection endpoint",
)
console.log(
"- POST /:external_user_id/:app/messages - App-specific message handler",
)
}
})
.on("error", (err) => {
console.error("Server startup error:", err)
})
}
// Only auto-start if this module is the entry point
// In ES modules, we need to check if the current file URL is the same as import.meta.url
// Check if this file is being run directly
const isMainModule = process.argv[1] === fileURLToPath(import.meta.url)
if (isMainModule) {
main().catch((error) => {
console.error("Fatal error in main():", error)
process.exit(1)
})
}