|
1 | | -import express, { Request, Response } from 'express'; |
2 | | -import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
3 | | -import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; |
4 | | -import { Logger } from '@aws-lambda-powertools/logger'; |
5 | | -import { pgMcp } from './pgMcp'; |
6 | | -// Import csrf from 'csurf' for CSRF protection |
7 | | -// This middleware adds CSRF protection to the DELETE route |
8 | | -import csrf from 'csurf'; |
9 | | -const csrfProtection = csrf({ cookie: true }); |
10 | | - |
11 | | -const logger = new Logger(); |
12 | | - |
13 | | -const app = express(); |
14 | | -app.use(express.json()); |
15 | | - |
16 | | -// Create server instance |
17 | | -const server = new McpServer({ |
18 | | - name: 'weather', |
19 | | - version: '1.0.0', |
20 | | - capabilities: {}, |
21 | | -}); |
22 | | - |
23 | | -app.post('/mcp', async (req: Request, res: Response) => { |
24 | | - // In stateless mode, create a new instance of transport and server for each request |
25 | | - // to ensure complete isolation. A single instance would cause request ID collisions |
26 | | - // when multiple clients connect concurrently. |
27 | | - |
28 | | - try { |
29 | | - const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({ |
30 | | - sessionIdGenerator: undefined, |
31 | | - }); |
32 | | - await server.connect(transport); |
33 | | - const body = req.body; |
34 | | - logger.info('Received MCP request:', body); |
35 | | - await transport.handleRequest(req, res, body); |
36 | | - res.on('close', () => { |
37 | | - logger.info('Request closed'); |
38 | | - transport.close(); |
39 | | - server.close(); |
40 | | - }); |
41 | | - } catch (error) { |
42 | | - console.error('Error handling MCP request:', error); |
43 | | - if (!res.headersSent) { |
44 | | - res.status(500).json({ |
45 | | - jsonrpc: '2.0', |
46 | | - error: { |
47 | | - code: -32603, |
48 | | - message: 'Internal server error', |
49 | | - }, |
50 | | - id: null, |
51 | | - }); |
52 | | - } |
53 | | - } |
54 | | -}); |
55 | | - |
56 | | -app.get('/mcp', async (req: Request, res: Response) => { |
57 | | - logger.info('Received GET MCP request'); |
58 | | - res.writeHead(405).end(JSON.stringify({ |
59 | | - jsonrpc: '2.0', |
60 | | - error: { |
61 | | - code: -32000, |
62 | | - message: 'Method not allowed.', |
63 | | - }, |
64 | | - id: null, |
65 | | - })); |
66 | | -}); |
67 | | - |
68 | | -app.delete('/mcp', csrfProtection, async (req: Request, res: Response) => { |
69 | | - logger.info('Received DELETE MCP request'); |
70 | | - res.writeHead(405).end(JSON.stringify({ |
71 | | - jsonrpc: '2.0', |
72 | | - error: { |
73 | | - code: -32000, |
74 | | - message: 'Method not allowed.', |
75 | | - }, |
76 | | - id: null, |
77 | | - })); |
78 | | -}); |
79 | | - |
80 | | -const resouces = [pgMcp.resources.schema, pgMcp.resources.status]; |
81 | | -resouces.forEach((resource) => { |
82 | | - server.resource( |
83 | | - resource.name, |
84 | | - resource.uri, |
85 | | - async () => { |
86 | | - const content = await resource.load(); |
87 | | - return { contents: [{ ...content, uri: resource.uri, mimeType: resource.mimeType }] }; |
88 | | - }, |
89 | | - ); |
90 | | -}); |
91 | | - |
92 | | -server.tool( |
93 | | - pgMcp.tools.query.name, |
94 | | - pgMcp.tools.query.description, |
95 | | - pgMcp.tools.query.parameters.shape, |
96 | | - pgMcp.tools.query.annotations, |
97 | | - async (params) => { |
98 | | - const resp = await pgMcp.tools.query.execute(params); |
99 | | - return { |
100 | | - ...resp, |
101 | | - }; |
102 | | - }); |
103 | | - |
104 | | -server.tool( |
105 | | - pgMcp.tools['list-tables'].name, |
106 | | - pgMcp.tools['list-tables'].description, |
107 | | - pgMcp.tools['list-tables'].parameters.shape, |
108 | | - pgMcp.tools['list-tables'].annotations, |
109 | | - async (params) => { |
110 | | - const resp = await pgMcp.tools['list-tables'].execute(params); |
111 | | - return { |
112 | | - ...resp, |
113 | | - }; |
114 | | - }); |
115 | | - |
116 | | -server.tool( |
117 | | - pgMcp.tools['describe-table'].name, |
118 | | - pgMcp.tools['describe-table'].description, |
119 | | - pgMcp.tools['describe-table'].parameters.shape, |
120 | | - pgMcp.tools['describe-table'].annotations, |
121 | | - async (params) => { |
122 | | - const resp = await pgMcp.tools['describe-table'].execute(params); |
123 | | - return { |
124 | | - ...resp, |
125 | | - }; |
126 | | - }); |
127 | | - |
128 | | -server.tool( |
129 | | - pgMcp.tools['call-procedure'].name, |
130 | | - pgMcp.tools['call-procedure'].description, |
131 | | - pgMcp.tools['call-procedure'].parameters.shape, |
132 | | - pgMcp.tools['call-procedure'].annotations, |
133 | | - async (params) => { |
134 | | - const resp = await pgMcp.tools['call-procedure'].execute(params); |
135 | | - return { |
136 | | - ...resp, |
137 | | - }; |
138 | | - }); |
139 | | - |
140 | | -server.tool( |
141 | | - pgMcp.tools['export-schema'].name, |
142 | | - pgMcp.tools['export-schema'].description, |
143 | | - pgMcp.tools['export-schema'].parameters.shape, |
144 | | - pgMcp.tools['export-schema'].annotations, |
145 | | - async (params) => { |
146 | | - const resp = await pgMcp.tools['export-schema'].execute(params); |
147 | | - return { |
148 | | - ...resp, |
149 | | - }; |
150 | | - }); |
| 1 | +import { handle } from 'hono/aws-lambda'; |
| 2 | +import { app } from './app'; |
151 | 3 |
|
152 | 4 | // Start the server |
153 | | -const PORT = 3000; |
154 | | -app.listen(PORT, () => { |
155 | | - logger.info(`MCP Stateless Streamable HTTP Server listening on port ${PORT}`); |
156 | | -}); |
| 5 | +export const handler = handle(app); |
0 commit comments