Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mcp): guard media type and share the tool registrar

Andrej1167ec6a9803
diff --git a/apps/api/src/mcp/index.ts b/apps/api/src/mcp/index.ts
index fd9ad45b6..b11d0ed3e 100644
--- a/apps/api/src/mcp/index.ts
+++ b/apps/api/src/mcp/index.ts
@@ -1,7 +1,10 @@
import { randomUUID } from "node:crypto";
import { McpServer as LegacyMcpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
-import { isLegacyRequest } from "@modelcontextprotocol/server";
+import {
+ isJsonContentType,
+ isLegacyRequest,
+} from "@modelcontextprotocol/server";
import { Hono } from "hono";
import { describeRoute, resolver, validator } from "hono-openapi";
import { auth } from "../auth";
@@ -23,7 +26,7 @@ import {
clientRegistrationSchema,
oauthErrorSchema,
} from "./schemas";
-import { registerMcpTools } from "./tools";
+import { registerMcpTools, toMcpToolRegistrar } from "./tools";
const apiUrl = (process.env.KANEO_API_URL || "http://localhost:1337").replace(
/\/api\/?$/,
@@ -42,21 +45,7 @@ function createMcpServerForUser(token: string): LegacyMcpServer {
name: "kaneo-mcp",
version: "1.0.0",
});
- registerMcpTools(
- {
- registerTool: (name, config, callback) =>
- server.registerTool(
- name,
- {
- description: config.description,
- inputSchema: config.inputSchema.shape,
- },
- (args) => callback(args),
- ),
- },
- apiUrl,
- token,
- );
+ registerMcpTools(toMcpToolRegistrar(server), apiUrl, token);
return server;
}
@@ -307,6 +296,10 @@ mcp.all("/mcp", async (c) => {
return c.json({ error: "Method not allowed" }, 405);
}
+ if (!isJsonContentType(c.req.header("content-type"))) {
+ return c.json({ error: "Unsupported Media Type" }, 415);
+ }
+
if (!(await isLegacyRequest(c.req.raw.clone()))) {
const modern = createModernMcpHandler(authResult.token, apiUrl);
return modern.fetch(c.req.raw);
diff --git a/apps/api/src/mcp/modern.ts b/apps/api/src/mcp/modern.ts
index 30f44c7f6..6de701209 100644
--- a/apps/api/src/mcp/modern.ts
+++ b/apps/api/src/mcp/modern.ts
@@ -1,5 +1,5 @@
import { createMcpHandler, McpServer } from "@modelcontextprotocol/server";
-import { registerMcpTools } from "./tools";
+import { registerMcpTools, toMcpToolRegistrar } from "./tools";
/** Create a stateless MCP 2026 handler with a fresh server per request. */
export function createModernMcpHandler(token: string, apiUrl: string) {
@@ -9,21 +9,7 @@ export function createModernMcpHandler(token: string, apiUrl: string) {
name: "kaneo-mcp",
version: "1.0.0",
});
- registerMcpTools(
- {
- registerTool: (name, config, callback) =>
- server.registerTool(
- name,
- {
- description: config.description,
- inputSchema: config.inputSchema.shape,
- },
- (args) => callback(args),
- ),
- },
- apiUrl,
- token,
- );
+ registerMcpTools(toMcpToolRegistrar(server), apiUrl, token);
return server;
},
{ legacy: "reject" },
diff --git a/apps/api/src/mcp/tools.ts b/apps/api/src/mcp/tools.ts
index 893aa1502..0cc733ed5 100644
--- a/apps/api/src/mcp/tools.ts
+++ b/apps/api/src/mcp/tools.ts
@@ -17,6 +17,28 @@ export type McpToolRegistrar = {
): unknown;
};
+type ShapeToolServer = {
+ registerTool(
+ name: string,
+ config: { description: string; inputSchema: z.ZodRawShape },
+ callback: (args: unknown) => Promise<McpToolResult>,
+ ): unknown;
+};
+
+export function toMcpToolRegistrar(server: ShapeToolServer): McpToolRegistrar {
+ return {
+ registerTool: (name, config, callback) =>
+ server.registerTool(
+ name,
+ {
+ description: config.description,
+ inputSchema: config.inputSchema.shape,
+ },
+ (args) => callback(args),
+ ),
+ };
+}
+
class ApiClient {
constructor(
private baseUrl: string,
@@ -200,9 +222,13 @@ export function registerMcpTools(
config: { description: string; inputSchema: InputSchema },
callback: (args: z.output<InputSchema>) => Promise<McpToolResult>,
) =>
- server.registerTool(name, config, (args) =>
- callback(config.inputSchema.parse(args)),
- );
+ server.registerTool(name, config, async (args) => {
+ const parsed = config.inputSchema.safeParse(args);
+ if (!parsed.success) {
+ return errorResult(z.prettifyError(parsed.error));
+ }
+ return callback(parsed.data);
+ });
registerTool(
"whoami",
diff --git a/tests/api/mcp-stateless.test.ts b/tests/api/mcp-stateless.test.ts
index 430485c03..c2fb2e5be 100644
--- a/tests/api/mcp-stateless.test.ts
+++ b/tests/api/mcp-stateless.test.ts
@@ -306,4 +306,66 @@ describe("MCP 2026-07-28 stateless HTTP", () => {
);
expect(authMocks.getSession).toHaveBeenCalledTimes(3);
});
+
+ it.each([
+ ["text/plain", "text/plain"],
+ ["a form post", "application/x-www-form-urlencoded"],
+ ["a missing header", undefined],
+ ])(
+ "rejects %s with 415 before either era is dispatched",
+ async (_label, contentType) => {
+ const response = await mcpRoutes.request("/mcp", {
+ method: "POST",
+ headers: {
+ accept: "application/json, text/event-stream",
+ authorization: "Bearer test-token",
+ ...(contentType ? { "content-type": contentType } : {}),
+ },
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }),
+ });
+
+ expect(response.status).toBe(415);
+ },
+ );
+
+ it("accepts a parameterised json media type", async () => {
+ const response = await mcpRoutes.request("/mcp", {
+ method: "POST",
+ headers: {
+ accept: "application/json, text/event-stream",
+ authorization: "Bearer test-token",
+ "content-type": "application/json; charset=utf-8",
+ },
+ body: JSON.stringify({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "initialize",
+ params: {
+ protocolVersion: "2025-06-18",
+ capabilities: {},
+ clientInfo: { name: "kaneo-test", version: "1.0.0" },
+ },
+ }),
+ });
+
+ expect(response.status).toBe(200);
+ });
+
+ it("returns a tool error rather than throwing when arguments fail validation", async () => {
+ const handler = createModernMcpHandler("test-token", "http://api.test");
+
+ const response = await handler.fetch(
+ modernRequest("tools/call", 1, {
+ name: "get_task",
+ arguments: { taskId: "" },
+ }),
+ );
+ const body = (await rpcBody(response)) as unknown as {
+ result: { content: Array<{ text: string }>; isError?: boolean };
+ };
+
+ expect(response.status).toBe(200);
+ expect(body.result.isError).toBe(true);
+ expect(body.result.content[0].text).toContain("taskId");
+ });
});