Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(api): let a tool send the body its route requires (#302)

Ahmad Othman Ammar Adi10daff5eb08f
diff --git a/apps/api/src/mcp/__tests__/unit/generate.test.ts b/apps/api/src/mcp/__tests__/unit/generate.test.ts
index 16551cb8..bcc5759b 100644
--- a/apps/api/src/mcp/__tests__/unit/generate.test.ts
+++ b/apps/api/src/mcp/__tests__/unit/generate.test.ts
@@ -101,3 +101,48 @@ describe('mcp tool table', () => {
expect(routeTools(app)[0]!.description).toBe('The long one.');
});
});
+
+// Builds a route whose hooks carry the given schemas, the way a real route declares
+// them, so the merged input schema can be read back off the generated tool.
+function toolWith(hooks: Record<string, unknown>, method = 'DELETE') {
+ const app = {
+ routes: [
+ {
+ method,
+ path: '/things/:thingId',
+ hooks: { ...hooks, detail: { summary: 'A thing', ...mcpTool('a_tool') } },
+ },
+ ],
+ } as unknown as McpApp;
+ return routeTools(app)[0]!;
+}
+
+describe('mcp tool input schema', () => {
+ // A body declared as a union has no properties of its own, so offering only the
+ // top level left the caller unable to send the discriminator the route requires.
+ it('offers every branch of a union body and requires what they share', () => {
+ const tool = toolWith({
+ body: {
+ anyOf: [
+ {
+ type: 'object',
+ properties: { mode: { const: 'move' }, targetId: { type: 'integer' } },
+ required: ['mode', 'targetId'],
+ },
+ { type: 'object', properties: { mode: { const: 'delete' } }, required: ['mode'] },
+ ],
+ },
+ });
+
+ expect(Object.keys(tool.inputSchema.properties)).toEqual(
+ expect.arrayContaining(['mode', 'targetId']),
+ );
+ expect(tool.inputSchema.required).toContain('mode');
+ expect(tool.inputSchema.required).not.toContain('targetId');
+ });
+
+ it('reports whether the route declares a body, whatever the method', () => {
+ expect(toolWith({ body: { type: 'object', properties: {} } }).hasBody).toBe(true);
+ expect(toolWith({}, 'GET').hasBody).toBe(false);
+ });
+});
diff --git a/apps/api/src/mcp/dispatch.ts b/apps/api/src/mcp/dispatch.ts
index 7da0312c..d5eccc6d 100644
--- a/apps/api/src/mcp/dispatch.ts
+++ b/apps/api/src/mcp/dispatch.ts
@@ -4,7 +4,6 @@ import { MCP_LOOPBACK_HEADER, setMcpOAuthToken } from '../shared/mcp-request';
import type { McpCredential } from './credential';
// Methods that carry a request body; the rest put their arguments in the query.
-const BODY_METHODS = new Set(['POST', 'PUT', 'PATCH']);
// Elysia's router needs a multi-label host to parse the path; a single-label host
// like "http://x" fails to route. localhost is never resolved (app.handle runs in
@@ -37,7 +36,7 @@ export async function dispatchTool(
delete rest[name];
}
- const hasBody = BODY_METHODS.has(tool.method);
+ const hasBody = tool.hasBody;
let url = `${BASE}${path}`;
let body: string | undefined;
if (hasBody) {
diff --git a/apps/api/src/mcp/generate.ts b/apps/api/src/mcp/generate.ts
index b45e48b0..6dccae8c 100644
--- a/apps/api/src/mcp/generate.ts
+++ b/apps/api/src/mcp/generate.ts
@@ -32,6 +32,9 @@ export interface McpRouteTool {
path: string;
// Path param names parsed from the template, e.g. ["projectKey"].
pathParams: string[];
+ // Whether the route declares a body schema. The method alone does not say: a
+ // DELETE carries one where the deletion needs an argument.
+ hasBody: boolean;
inputSchema: McpInputSchema;
annotations: McpToolAnnotations;
}
@@ -83,15 +86,32 @@ function extractPathParams(path: string): string[] {
// schema for the tool's arguments. Path params are always added as required: a
// route often omits an explicit `params` schema for a string id, which would
// otherwise leave the path param out of the tool's arguments entirely.
+interface SchemaShape {
+ properties?: Record<string, unknown>;
+ required?: unknown;
+ anyOf?: SchemaShape[];
+ oneOf?: SchemaShape[];
+}
+
function mergeInputSchema(hooks: Record<string, unknown>, pathParams: string[]): McpInputSchema {
const properties: Record<string, unknown> = {};
const required: string[] = [];
for (const key of ['params', 'query', 'body'] as const) {
- const schema = hooks[key] as
- { properties?: Record<string, unknown>; required?: unknown } | undefined;
- if (!schema?.properties) continue;
- Object.assign(properties, schema.properties);
- if (Array.isArray(schema.required)) required.push(...(schema.required as string[]));
+ const schema = hooks[key] as SchemaShape | undefined;
+ if (!schema) continue;
+ // A body declared as a union arrives as anyOf/oneOf with no properties of its
+ // own. Offer every branch's properties and require only what all of them do, so
+ // the caller sees the discriminator; the route still validates the combination.
+ const branches = schema.anyOf ?? schema.oneOf;
+ const parts = schema.properties ? [schema] : (branches ?? []);
+ let common: string[] | null = null;
+ for (const part of parts) {
+ if (!part.properties) continue;
+ Object.assign(properties, part.properties);
+ const names = Array.isArray(part.required) ? (part.required as string[]) : [];
+ common = common === null ? names : common.filter((n) => names.includes(n));
+ }
+ if (common) required.push(...common);
}
for (const name of pathParams) {
if (!(name in properties)) {
@@ -140,6 +160,7 @@ function generateRouteTools(app: McpApp): McpRouteTool[] {
method: route.method,
path: route.path,
pathParams,
+ hasBody: hooks.body != null,
inputSchema: mergeInputSchema(hooks, pathParams),
// Every tool acts on this tracker's own data and reaches nothing outside it,
// so openWorldHint is false throughout; the route may still override it.