|
| 1 | +/** |
| 2 | + * Standalone HTTP server for MCP mock testing. |
| 3 | + * Imports createMcpApp from stackone-ai-node vendor submodule. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * bun run tests/mocks/serve.ts [port] |
| 7 | + */ |
| 8 | +import { Hono } from "hono"; |
| 9 | +import { cors } from "hono/cors"; |
| 10 | +import { |
| 11 | + accountMcpTools, |
| 12 | + createMcpApp, |
| 13 | + defaultMcpTools, |
| 14 | + exampleBamboohrTools, |
| 15 | + mixedProviderTools, |
| 16 | +} from "../../vendor/stackone-ai-node/mocks/mcp-server"; |
| 17 | + |
| 18 | +const port = parseInt(process.env.PORT || Bun.argv[2] || "8787", 10); |
| 19 | + |
| 20 | +// Create the MCP app with all test tool configurations |
| 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 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +// Create the main app with CORS and mount the MCP app |
| 35 | +const app = new Hono(); |
| 36 | + |
| 37 | +// Add CORS for cross-origin requests |
| 38 | +app.use("/*", cors()); |
| 39 | + |
| 40 | +// Health check endpoint |
| 41 | +app.get("/health", (c) => c.json({ status: "ok" })); |
| 42 | + |
| 43 | +// Mount the MCP app (handles /mcp endpoint) |
| 44 | +app.route("/", mcpApp); |
| 45 | + |
| 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({ 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 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +console.log(`MCP Mock Server starting on port ${port}...`); |
| 121 | + |
| 122 | +export default { |
| 123 | + port, |
| 124 | + fetch: app.fetch, |
| 125 | +}; |
| 126 | + |
| 127 | +console.log(`MCP Mock Server running at http://localhost:${port}`); |
| 128 | +console.log("Endpoints:"); |
| 129 | +console.log(` - GET /health - Health check`); |
| 130 | +console.log(` - ALL /mcp - MCP protocol endpoint`); |
| 131 | +console.log(` - POST /actions/rpc - RPC execution endpoint`); |
0 commit comments