agents
agents copied to clipboard
MCP unstable_getAITools doesn't seem to work when there are no args
I created an MCPAgent with a tool with no args:
export class SpecialMCP extends McpAgent<Env, State, Props> {
server = new McpServer({ name: "SpecialMCP", version: "0.1.0" });
async init() {
this.server.tool(
"thing",
"description",
async () => {
...
return {
content: ...
};
}
);
}
}
...and an AIChatAgent with an mcp
export default class SpecialAgent extends AIChatAgent {
mcp = new MCPClientManager("special", "1.0.0");
async onStart() {
await this.mcp.connect(`http://localhost:5173/mcp`);
}
async onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>) {
return createDataStreamResponse({
execute: async (dataStream) => {
try {
const tools = this.mcp.unstable_getAITools();
// Fix all MCP tools to have proper Zod schemas << this part is important
Object.keys(tools).forEach((toolKey) => {
const tool = tools[toolKey];
if (
tool.parameters.jsonSchema &&
tool.parameters.jsonSchema.type === "object" &&
!tool.parameters.jsonSchema.properties
) {
console.log(
`Fixing MCP tool ${toolKey} parameters to use empty Zod object`
);
tool.parameters = z.object({});
}
});
const stream = streamText({
model: openai("gpt-4o-mini"),
system: `You are a helpful assistant`,
messages: this.messages,
maxSteps: 5,
tools,
});
stream.mergeIntoDataStream(dataStream);
} catch (error) {
console.error("Error in onChatMessage:", error);
}
},
});
}
}
the chat would hang if i didn't add the part about modifying the zod
I think this is related to the openai provider specifically. It requires properties in the tool's schema object. I've run into the same thing when testing the cloudflare's mcp servers.
Here is snippet of the error error: APICallError [AI_APICallError]: Invalid schema for function '6TFK2xvY_migrate_pages_to_workers_guide': In context=(), object schema missing properties
I think this is somehow known problem in MCP clients, as cursor adds "random_string" as property for tools without any parameters 🤷