Skip to content

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

fix(assistant): fix silent MCP tool schema validation (#15889)

Ben Bachemf5a794cf271f
diff --git a/packages/shared/src/in-app-agent/server/tools.ts b/packages/shared/src/in-app-agent/server/tools.ts
index 5ccc7f6a60..c866b8b2c6 100644
--- a/packages/shared/src/in-app-agent/server/tools.ts
+++ b/packages/shared/src/in-app-agent/server/tools.ts
@@ -549,6 +549,26 @@ export type CompletedInAppAgentMcpToolCall = {
createdAt: Date;
};
+type WrappableMcpTool = Pick<
+ Tool,
+ "execute" | "inputSchema" | "toModelOutput"
+> &
+ Required<Pick<Tool, "execute" | "inputSchema">>;
+
+function isWrappableMcpTool(tool: unknown): tool is WrappableMcpTool {
+ return (
+ typeof tool === "object" &&
+ tool !== null &&
+ "execute" in tool &&
+ typeof tool.execute === "function" &&
+ "inputSchema" in tool &&
+ tool.inputSchema !== undefined &&
+ (!("toModelOutput" in tool) ||
+ tool.toModelOutput === undefined ||
+ typeof tool.toModelOutput === "function")
+ );
+}
+
export function withOptionalSilentMcpOutput(params: {
tools: Record<string, unknown> | undefined;
sandbox?: InAppAgentSandbox;
@@ -560,14 +580,13 @@ export function withOptionalSilentMcpOutput(params: {
return [toolName, tool];
}
- if (!(tool instanceof Tool)) {
+ // MCPClient may construct tools with a different @mastra/core module
+ // instance, so instanceof Tool is not reliable across package realms.
+ if (!isWrappableMcpTool(tool)) {
return [toolName, tool];
}
const { execute, inputSchema, toModelOutput } = tool;
- if (!execute || !inputSchema) {
- return [toolName, tool];
- }
if (inputSchema instanceof z.ZodObject) {
tool.inputSchema = inputSchema.extend({
diff --git a/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts
index 394c71f323..ec9aaf5919 100644
--- a/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts
+++ b/web/src/__tests__/server/unit/in-app-agent-sandbox.servertest.ts
@@ -11,6 +11,7 @@ import {
} from "@langfuse/shared/in-app-agent/server/persistence";
import { createInAppAgentSandbox } from "@langfuse/shared/in-app-agent/server/sandbox";
import { withOptionalSilentMcpOutput } from "@langfuse/shared/in-app-agent/server/tools";
+import { listObservationsTool } from "@/src/features/mcp/features/observations/tools/listObservations";
describe("in-app agent sandbox", () => {
it("redacts silent MCP output from persisted conversation display", async () => {
@@ -356,24 +357,22 @@ describe("in-app agent sandbox", () => {
);
});
- it("advertises silent MCP output for JSON Schema tools", () => {
+ it("supports silent output for the listObservations JSON Schema", async () => {
+ let receivedInput: unknown;
const tools = withOptionalSilentMcpOutput({
tools: {
- search: new Tool({
- id: "search",
- description: "Search",
- inputSchema: {
- type: "object",
- properties: {
- query: { type: "string" },
+ // MCP tools can originate from a different @mastra/core module instance.
+ listObservations: {
+ ...new Tool({
+ id: "listObservations",
+ description: listObservationsTool.description,
+ inputSchema: listObservationsTool.inputSchema,
+ execute: async (input) => {
+ receivedInput = input;
+ return { data: [] };
},
- required: ["query"],
- additionalProperties: false,
- },
- execute: async (input: { query: string }) => ({
- result: input.query,
}),
- }),
+ },
},
sandbox: {
async read() {
@@ -391,11 +390,14 @@ describe("in-app agent sandbox", () => {
},
});
- if (!tools.search.inputSchema) {
+ if (!tools.listObservations.inputSchema) {
throw new Error("Expected an input schema");
}
- expect(standardSchemaToJSONSchema(tools.search.inputSchema)).toMatchObject({
+ expect(
+ standardSchemaToJSONSchema(tools.listObservations.inputSchema),
+ ).toMatchObject({
+ additionalProperties: false,
properties: {
silent: {
type: "boolean",
@@ -404,6 +406,17 @@ describe("in-app agent sandbox", () => {
},
},
});
+
+ await expect(
+ tools.listObservations.execute?.(
+ { limit: 10, silent: true },
+ {} as never,
+ ),
+ ).resolves.toEqual({
+ type: "silent-mcp-output",
+ output: { data: [] },
+ });
+ expect(receivedInput).toEqual({ limit: 10 });
});
it("returns normal MCP output when silent is omitted", async () => {