Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(mcp): inject root "type: object" so Tool.inputSchema satisfies MCP SDK (#13805)

taka7c06d21e6e68
diff --git a/web/src/__tests__/server/unit/mcp-define-tool.servertest.ts b/web/src/__tests__/server/unit/mcp-define-tool.servertest.ts
index 889e211afc..fa6460dcc9 100644
--- a/web/src/__tests__/server/unit/mcp-define-tool.servertest.ts
+++ b/web/src/__tests__/server/unit/mcp-define-tool.servertest.ts
@@ -1,7 +1,6 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { defineTool } from "../../../features/mcp/core/define-tool";
-import { updateScoreConfigTool } from "../../../features/mcp/features/scores/tools/updateScoreConfig";
describe("defineTool", () => {
it("accepts object intersections emitted as JSON Schema allOf", () => {
@@ -23,7 +22,53 @@ describe("defineTool", () => {
expect(tool.inputSchema).toHaveProperty("allOf");
});
- it("defines updateScoreConfig without omitting from a refined schema", () => {
- expect(updateScoreConfigTool.name).toBe("updateScoreConfig");
+ // Regression test for MCP TS SDK compatibility: ToolSchema.inputSchema is
+ // validated with `z.literal("object")` on the root `type`, so clients drop
+ // tools whose root schema omits it. Zod emits intersections as bare `allOf`,
+ // so defineTool must inject `type: "object"`.
+ it("injects root type: 'object' for intersection schemas (MCP SDK requires it)", () => {
+ const schema = z.union([
+ z.object({ scoreId: z.string() }),
+ z.object({ configId: z.string() }),
+ ]);
+
+ const [tool] = defineTool({
+ name: "objectUnionTool",
+ description: "",
+ baseSchema: schema,
+ inputSchema: schema,
+ handler: async (input) => input,
+ });
+
+ expect(tool.inputSchema.type).toBe("object");
+ expect(tool.inputSchema).toHaveProperty("anyOf");
+ });
+
+ it("preserves root type: 'object' for plain object schemas", () => {
+ const schema = z.object({ name: z.string() });
+
+ const [tool] = defineTool({
+ name: "plainTool",
+ description: "",
+ baseSchema: schema,
+ inputSchema: schema,
+ handler: async (input) => input,
+ });
+
+ expect(tool.inputSchema.type).toBe("object");
+ });
+
+ it("rejects unions with non-object branches", () => {
+ const schema = z.union([z.string(), z.object({ id: z.string() })]);
+
+ expect(() =>
+ defineTool({
+ name: "mixedUnionTool",
+ description: "",
+ baseSchema: schema,
+ inputSchema: schema,
+ handler: async (input) => input,
+ }),
+ ).toThrow("Expected object or union schema");
});
});
diff --git a/web/src/features/mcp/core/define-tool.ts b/web/src/features/mcp/core/define-tool.ts
index 50127535c2..f4151c0f92 100644
--- a/web/src/features/mcp/core/define-tool.ts
+++ b/web/src/features/mcp/core/define-tool.ts
@@ -65,16 +65,19 @@ type JsonSchemaObject = Record<string, unknown>;
function isObjectLikeJsonSchema(schema: JsonSchemaObject): boolean {
if (schema.type === "object") return true;
- if ("oneOf" in schema || "anyOf" in schema || "discriminator" in schema) {
- return true;
- }
-
- if (Array.isArray(schema.allOf)) {
- return schema.allOf.every(
+ const subSchemas = (() => {
+ if ("oneOf" in schema && Array.isArray(schema.oneOf)) return schema.oneOf;
+ if ("anyOf" in schema && Array.isArray(schema.anyOf)) return schema.anyOf;
+ if ("allOf" in schema && Array.isArray(schema.allOf)) return schema.allOf;
+ return [];
+ })();
+
+ if (subSchemas.length > 0) {
+ return subSchemas.every(
(subSchema) =>
typeof subSchema === "object" &&
subSchema !== null &&
- isObjectLikeJsonSchema(subSchema as JsonSchemaObject),
+ isObjectLikeJsonSchema(subSchema),
);
}
@@ -138,11 +141,21 @@ export function defineTool<TInput>(
);
}
+ // The MCP TypeScript SDK validates Tool.inputSchema.type as `z.literal("object")`,
+ // so clients reject any tool whose root schema lacks `type: "object"`. Zod's
+ // JSON Schema converter omits the root `type` for intersection/union schemas
+ // (emitting allOf/oneOf/anyOf instead), so we inject it here. JSON Schema
+ // draft-7 allows `type` alongside these keywords — all constraints must hold.
+ const normalizedJsonSchema: JsonSchemaObject =
+ (jsonSchema as JsonSchemaObject).type === "object"
+ ? (jsonSchema as JsonSchemaObject)
+ : { ...(jsonSchema as JsonSchemaObject), type: "object" };
+
// Build tool definition
const toolDefinition: ToolDefinition = {
name,
description,
- inputSchema: jsonSchema,
+ inputSchema: normalizedJsonSchema,
};
// Add annotations if provided