CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
mcp.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 2c2163e | 1 | /** |
| 2 | * Model Context Protocol HTTP transport. | |
| 3 | * | |
| 4 | * POST /mcp — JSON-RPC 2.0 requests; body is a single request | |
| 5 | * or an array (batch). Response shape mirrors. | |
| 6 | * GET /mcp — Lightweight discovery: returns server info + | |
| 7 | * protocol version + tool count. | |
| 8 | * | |
| 9 | * Auth: softAuth — `userId` in the McpContext is the cookie/PAT/OAuth | |
| 10 | * user when present, null otherwise. v1 tools are read-only and public- | |
| 11 | * only, so anonymous works; write tools (v2) will require requireAuth + | |
| 12 | * write-access on the target repo. | |
| 13 | * | |
| 14 | * Streamable-HTTP-mode is the recommended MCP transport for stateless | |
| 15 | * cloud servers. We don't emit server-sent notifications yet, so the | |
| 16 | * route is plain JSON in / JSON out. | |
| 17 | */ | |
| 18 | ||
| 19 | import { Hono } from "hono"; | |
| 20 | import { softAuth } from "../middleware/auth"; | |
| 21 | import type { AuthEnv } from "../middleware/auth"; | |
| 22 | import { | |
| 23 | routeMcpRequest, | |
| 24 | MCP_PROTOCOL_VERSION, | |
| 25 | MCP_SERVER_NAME, | |
| 26 | MCP_SERVER_VERSION, | |
| 27 | } from "../lib/mcp"; | |
| 28 | import { defaultTools } from "../lib/mcp-tools"; | |
| 29 | ||
| 30 | const mcp = new Hono<AuthEnv>(); | |
| 31 | ||
| 32 | mcp.use("*", softAuth); | |
| 33 | ||
| 34 | mcp.get("/mcp", (c) => { | |
| 35 | const tools = defaultTools(); | |
| 36 | return c.json({ | |
| 37 | protocolVersion: MCP_PROTOCOL_VERSION, | |
| 38 | serverInfo: { name: MCP_SERVER_NAME, version: MCP_SERVER_VERSION }, | |
| 39 | transport: "http", | |
| 40 | toolCount: Object.keys(tools).length, | |
| 41 | docs: | |
| 42 | "POST /mcp with a JSON-RPC 2.0 envelope to call. See https://spec.modelcontextprotocol.io/", | |
| 43 | }); | |
| 44 | }); | |
| 45 | ||
| 46 | mcp.post("/mcp", async (c) => { | |
| 47 | const user = c.get("user") ?? null; | |
| 48 | const ctx = { userId: user?.id ?? null }; | |
| 49 | const tools = defaultTools(); | |
| 50 | ||
| 51 | let body: unknown; | |
| 52 | try { | |
| 53 | body = await c.req.json(); | |
| 54 | } catch { | |
| 55 | return c.json( | |
| 56 | { | |
| 57 | jsonrpc: "2.0", | |
| 58 | id: null, | |
| 59 | error: { code: -32700, message: "Parse error" }, | |
| 60 | }, | |
| 61 | 400 | |
| 62 | ); | |
| 63 | } | |
| 64 | ||
| 65 | if (Array.isArray(body)) { | |
| 66 | // Batched request — pass each through, drop nulls (notifications). | |
| 67 | const out = await Promise.all( | |
| 68 | body.map((entry) => routeMcpRequest(entry, { ctx, tools })) | |
| 69 | ); | |
| 70 | const filtered = out.filter((r): r is NonNullable<typeof r> => r !== null); | |
| 71 | if (filtered.length === 0) return c.body(null, 204); | |
| 72 | return c.json(filtered); | |
| 73 | } | |
| 74 | ||
| 75 | const result = await routeMcpRequest(body, { ctx, tools }); | |
| 76 | if (result === null) { | |
| 77 | // Notification — no response body, 204. | |
| 78 | return c.body(null, 204); | |
| 79 | } | |
| 80 | return c.json(result); | |
| 81 | }); | |
| 82 | ||
| 83 | export default mcp; |