Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): return invalid_input for MCP tool arguments that fail the input schema (#4329)

Rui5919154bf1ef
diff --git a/.changeset/mcp-invalid-input.md b/.changeset/mcp-invalid-input.md
new file mode 100644
index 000000000..584dba92b
--- /dev/null
+++ b/.changeset/mcp-invalid-input.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+MCP `tools/call` requests whose arguments don't match the tool's input schema now return `structuredContent.error` with code `invalid_input`, like every other rejected call, instead of only a text message.
diff --git a/packages/eve/scripts/vendor-compiled/@modelcontextprotocol/server.mjs b/packages/eve/scripts/vendor-compiled/@modelcontextprotocol/server.mjs
index c02399f5e..8d86b86d9 100644
--- a/packages/eve/scripts/vendor-compiled/@modelcontextprotocol/server.mjs
+++ b/packages/eve/scripts/vendor-compiled/@modelcontextprotocol/server.mjs
@@ -27,9 +27,27 @@ export interface McpToolAnnotations {
readonly readOnlyHint?: boolean;
}
+export interface StandardSchemaIssue {
+ readonly message: string;
+ readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[];
+}
+
+export type StandardSchemaResult<TOutput> =
+ | { readonly value: TOutput; readonly issues?: undefined }
+ | { readonly issues: readonly StandardSchemaIssue[] };
+
export interface StandardSchemaWithJSON<TInput = unknown, TOutput = TInput> {
readonly "~standard": {
+ readonly version: 1;
+ readonly vendor: string;
readonly types?: { readonly input: TInput; readonly output: TOutput };
+ readonly validate: (
+ value: unknown,
+ ) => StandardSchemaResult<TOutput> | Promise<StandardSchemaResult<TOutput>>;
+ readonly jsonSchema: {
+ readonly input: (options: { readonly target: string }) => Record<string, unknown>;
+ readonly output: (options: { readonly target: string }) => Record<string, unknown>;
+ };
};
}
diff --git a/packages/eve/src/internal/mcp/streamable-http-server.test.ts b/packages/eve/src/internal/mcp/streamable-http-server.test.ts
index c0031170f..da4197857 100644
--- a/packages/eve/src/internal/mcp/streamable-http-server.test.ts
+++ b/packages/eve/src/internal/mcp/streamable-http-server.test.ts
@@ -253,7 +253,7 @@ describe("stateless MCP Streamable HTTP server", () => {
);
});
- it("enforces the advertised tool input schema before dispatch", async () => {
+ it("rejects arguments outside the advertised input schema as invalid_input before dispatch", async () => {
const { call, handle } = server();
const response = await handle(
modernRequest({
@@ -267,8 +267,17 @@ describe("stateless MCP Streamable HTTP server", () => {
expect(await jsonRpcResponse(response)).toMatchObject({
id: "invalid-call",
result: {
- content: [{ text: expect.stringContaining("Input validation error"), type: "text" }],
+ content: [
+ { text: expect.stringContaining("Invalid arguments for tool echo"), type: "text" },
+ ],
isError: true,
+ structuredContent: {
+ error: {
+ code: "invalid_input",
+ message: expect.stringContaining("value"),
+ retryable: false,
+ },
+ },
},
});
expect(call).not.toHaveBeenCalled();
diff --git a/packages/eve/src/internal/mcp/streamable-http-server.ts b/packages/eve/src/internal/mcp/streamable-http-server.ts
index 7c1258be6..5722b7b74 100644
--- a/packages/eve/src/internal/mcp/streamable-http-server.ts
+++ b/packages/eve/src/internal/mcp/streamable-http-server.ts
@@ -2,6 +2,7 @@ import {
createMcpHandler,
McpServer,
type McpToolAnnotations,
+ type StandardSchemaIssue,
type StandardSchemaWithJSON,
} from "#compiled/@modelcontextprotocol/server/index.js";
@@ -101,23 +102,58 @@ export function defineMcpTool<
...(input.definition.description === undefined
? {}
: { description: input.definition.description }),
- inputSchema: input.definition.inputSchema,
+ inputSchema: advertiseOnly(input.definition.inputSchema),
...(input.definition.outputSchema === undefined
? {}
: { outputSchema: input.definition.outputSchema }),
},
- async (value, context) =>
- await callTool(
+ async (value, context) => {
+ const checked = await input.definition.inputSchema["~standard"].validate(value);
+ if (checked.issues !== undefined) {
+ return toolError({
+ code: "invalid_input",
+ message: `Invalid arguments for tool ${input.definition.name}: ${checked.issues.map(formatIssue).join(", ")}`,
+ retryable: false,
+ });
+ }
+ return await callTool(
input.call,
- value as InferSchemaOutput<TInputSchema>,
+ checked.value as InferSchemaOutput<TInputSchema>,
context.mcpReq.signal,
auth,
- ),
+ );
+ },
);
},
};
}
+/**
+ * The SDK both advertises and enforces a tool's input schema, but answers a
+ * rejected call with bare text. This copy keeps the advertised JSON Schema and
+ * accepts any arguments, so `defineMcpTool` checks them against the real
+ * schema and returns an `invalid_input` error like every other rejection.
+ */
+function advertiseOnly(schema: StandardSchemaWithJSON): StandardSchemaWithJSON {
+ const standard = schema["~standard"];
+ return {
+ "~standard": {
+ jsonSchema: standard.jsonSchema,
+ validate: (value) => ({ value }),
+ vendor: standard.vendor,
+ version: standard.version,
+ },
+ };
+}
+
+function formatIssue(issue: StandardSchemaIssue): string {
+ if (issue.path === undefined || issue.path.length === 0) return issue.message;
+ const path = issue.path
+ .map((segment) => String(typeof segment === "object" ? segment.key : segment))
+ .join(".");
+ return `${path}: ${issue.message}`;
+}
+
interface McpStreamableHttpServerOptions {
readonly name: string;
readonly version: string;
diff --git a/packages/eve/src/public/channels/mcp.test.ts b/packages/eve/src/public/channels/mcp.test.ts
index cafbe489f..44375b014 100644
--- a/packages/eve/src/public/channels/mcp.test.ts
+++ b/packages/eve/src/public/channels/mcp.test.ts
@@ -234,8 +234,11 @@ describe("mcpChannel", () => {
);
await expect(jsonRpcResponse(oversizedStart)).resolves.toMatchObject({
result: {
- content: [{ text: expect.stringContaining("Input validation error"), type: "text" }],
+ content: [
+ { text: expect.stringContaining("Invalid arguments for tool agent_start"), type: "text" },
+ ],
isError: true,
+ structuredContent: { error: { code: "invalid_input", retryable: false } },
},
});
@@ -255,7 +258,7 @@ describe("mcpChannel", () => {
routeArgs(createSession),
);
await expect(jsonRpcResponse(oversizedUpdate)).resolves.toMatchObject({
- result: { isError: true },
+ result: { isError: true, structuredContent: { error: { code: "invalid_input" } } },
});
expect(createSession).not.toHaveBeenCalled();
});