|
5 | 5 | * Usage: |
6 | 6 | * bun run tests/mocks/serve.ts [port] |
7 | 7 | */ |
8 | | -import { Hono } from 'hono'; |
9 | | -import { cors } from 'hono/cors'; |
| 8 | +import { Hono } from "hono"; |
| 9 | +import { cors } from "hono/cors"; |
10 | 10 | import { |
11 | | - accountMcpTools, |
12 | | - createMcpApp, |
13 | | - defaultMcpTools, |
14 | | - exampleBamboohrTools, |
15 | | - mixedProviderTools, |
16 | | -} from '../../vendor/stackone-ai-node/mocks/mcp-server'; |
| 11 | + accountMcpTools, |
| 12 | + createMcpApp, |
| 13 | + defaultMcpTools, |
| 14 | + exampleBamboohrTools, |
| 15 | + mixedProviderTools, |
| 16 | +} from "../../vendor/stackone-ai-node/mocks/mcp-server"; |
17 | 17 |
|
18 | | -const port = parseInt(process.env.PORT || Bun.argv[2] || '8787', 10); |
| 18 | +const port = parseInt(process.env.PORT || Bun.argv[2] || "8787", 10); |
19 | 19 |
|
20 | 20 | // Create the MCP app with all test tool configurations |
21 | 21 | const mcpApp = createMcpApp({ |
22 | | - accountTools: { |
23 | | - default: defaultMcpTools, |
24 | | - acc1: accountMcpTools.acc1, |
25 | | - acc2: accountMcpTools.acc2, |
26 | | - acc3: accountMcpTools.acc3, |
27 | | - 'test-account': accountMcpTools['test-account'], |
28 | | - mixed: mixedProviderTools, |
29 | | - 'your-bamboohr-account-id': exampleBamboohrTools, |
30 | | - 'your-stackone-account-id': exampleBamboohrTools, |
31 | | - }, |
| 22 | + accountTools: { |
| 23 | + default: defaultMcpTools, |
| 24 | + acc1: accountMcpTools.acc1, |
| 25 | + acc2: accountMcpTools.acc2, |
| 26 | + acc3: accountMcpTools.acc3, |
| 27 | + "test-account": accountMcpTools["test-account"], |
| 28 | + mixed: mixedProviderTools, |
| 29 | + "your-bamboohr-account-id": exampleBamboohrTools, |
| 30 | + "your-stackone-account-id": exampleBamboohrTools, |
| 31 | + }, |
32 | 32 | }); |
33 | 33 |
|
34 | 34 | // Create the main app with CORS and mount the MCP app |
35 | 35 | const app = new Hono(); |
36 | 36 |
|
37 | 37 | // Add CORS for cross-origin requests |
38 | | -app.use('/*', cors()); |
| 38 | +app.use("/*", cors()); |
39 | 39 |
|
40 | 40 | // Health check endpoint |
41 | | -app.get('/health', (c) => c.json({ status: 'ok' })); |
| 41 | +app.get("/health", (c) => c.json({ status: "ok" })); |
42 | 42 |
|
43 | 43 | // Mount the MCP app (handles /mcp endpoint) |
44 | | -app.route('/', mcpApp); |
| 44 | +app.route("/", mcpApp); |
45 | 45 |
|
46 | 46 | // RPC endpoint for tool execution |
47 | | -app.post('/actions/rpc', async (c) => { |
48 | | - const authHeader = c.req.header('Authorization'); |
49 | | - const accountIdHeader = c.req.header('x-account-id'); |
50 | | - |
51 | | - // Check for authentication |
52 | | - if (!authHeader || !authHeader.startsWith('Basic ')) { |
53 | | - return c.json( |
54 | | - { error: 'Unauthorized', message: 'Missing or invalid authorization header' }, |
55 | | - 401, |
56 | | - ); |
57 | | - } |
58 | | - |
59 | | - const body = (await c.req.json()) as { |
60 | | - action?: string; |
61 | | - body?: Record<string, unknown>; |
62 | | - headers?: Record<string, string>; |
63 | | - path?: Record<string, string>; |
64 | | - query?: Record<string, string>; |
65 | | - }; |
66 | | - |
67 | | - // Validate action is provided |
68 | | - if (!body.action) { |
69 | | - return c.json({ error: 'Bad Request', message: 'Action is required' }, 400); |
70 | | - } |
71 | | - |
72 | | - // Test action to verify x-account-id is sent as HTTP header |
73 | | - if (body.action === 'test_account_id_header') { |
74 | | - return c.json({ |
75 | | - data: { |
76 | | - httpHeader: accountIdHeader, |
77 | | - bodyHeader: body.headers?.['x-account-id'], |
78 | | - }, |
79 | | - }); |
80 | | - } |
81 | | - |
82 | | - // Return mock response based on action |
83 | | - if (body.action === 'bamboohr_get_employee') { |
84 | | - return c.json({ |
85 | | - data: { |
86 | | - id: body.path?.id || 'test-id', |
87 | | - name: 'Test Employee', |
88 | | - ...body.body, |
89 | | - }, |
90 | | - }); |
91 | | - } |
92 | | - |
93 | | - if (body.action === 'bamboohr_list_employees') { |
94 | | - return c.json({ |
95 | | - data: [ |
96 | | - { id: '1', name: 'Employee 1' }, |
97 | | - { id: '2', name: 'Employee 2' }, |
98 | | - ], |
99 | | - }); |
100 | | - } |
101 | | - |
102 | | - if (body.action === 'test_error_action') { |
103 | | - return c.json( |
104 | | - { error: 'Internal Server Error', message: 'Test error response' }, |
105 | | - 500, |
106 | | - ); |
107 | | - } |
108 | | - |
109 | | - // Default response for other actions |
110 | | - return c.json({ |
111 | | - data: { |
112 | | - action: body.action, |
113 | | - received: { |
114 | | - body: body.body, |
115 | | - headers: body.headers, |
116 | | - path: body.path, |
117 | | - query: body.query, |
118 | | - }, |
119 | | - }, |
120 | | - }); |
| 47 | +app.post("/actions/rpc", async (c) => { |
| 48 | + const authHeader = c.req.header("Authorization"); |
| 49 | + const accountIdHeader = c.req.header("x-account-id"); |
| 50 | + |
| 51 | + // Check for authentication |
| 52 | + if (!authHeader || !authHeader.startsWith("Basic ")) { |
| 53 | + return c.json( |
| 54 | + { error: "Unauthorized", message: "Missing or invalid authorization header" }, |
| 55 | + 401, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + const body = (await c.req.json()) as { |
| 60 | + action?: string; |
| 61 | + body?: Record<string, unknown>; |
| 62 | + headers?: Record<string, string>; |
| 63 | + path?: Record<string, string>; |
| 64 | + query?: Record<string, string>; |
| 65 | + }; |
| 66 | + |
| 67 | + // Validate action is provided |
| 68 | + if (!body.action) { |
| 69 | + return c.json({ error: "Bad Request", message: "Action is required" }, 400); |
| 70 | + } |
| 71 | + |
| 72 | + // Test action to verify x-account-id is sent as HTTP header |
| 73 | + if (body.action === "test_account_id_header") { |
| 74 | + return c.json({ |
| 75 | + data: { |
| 76 | + httpHeader: accountIdHeader, |
| 77 | + bodyHeader: body.headers?.["x-account-id"], |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + // Return mock response based on action |
| 83 | + if (body.action === "bamboohr_get_employee") { |
| 84 | + return c.json({ |
| 85 | + data: { |
| 86 | + id: body.path?.id || "test-id", |
| 87 | + name: "Test Employee", |
| 88 | + ...body.body, |
| 89 | + }, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + if (body.action === "bamboohr_list_employees") { |
| 94 | + return c.json({ |
| 95 | + data: [ |
| 96 | + { id: "1", name: "Employee 1" }, |
| 97 | + { id: "2", name: "Employee 2" }, |
| 98 | + ], |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + if (body.action === "test_error_action") { |
| 103 | + return c.json({ error: "Internal Server Error", message: "Test error response" }, 500); |
| 104 | + } |
| 105 | + |
| 106 | + // Default response for other actions |
| 107 | + return c.json({ |
| 108 | + data: { |
| 109 | + action: body.action, |
| 110 | + received: { |
| 111 | + body: body.body, |
| 112 | + headers: body.headers, |
| 113 | + path: body.path, |
| 114 | + query: body.query, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }); |
121 | 118 | }); |
122 | 119 |
|
123 | 120 | console.log(`MCP Mock Server starting on port ${port}...`); |
124 | 121 |
|
125 | 122 | export default { |
126 | | - port, |
127 | | - fetch: app.fetch, |
| 123 | + port, |
| 124 | + fetch: app.fetch, |
128 | 125 | }; |
129 | 126 |
|
130 | 127 | console.log(`MCP Mock Server running at http://localhost:${port}`); |
131 | | -console.log('Endpoints:'); |
| 128 | +console.log("Endpoints:"); |
132 | 129 | console.log(` - GET /health - Health check`); |
133 | 130 | console.log(` - ALL /mcp - MCP protocol endpoint`); |
134 | 131 | console.log(` - POST /actions/rpc - RPC execution endpoint`); |
0 commit comments