agents icon indicating copy to clipboard operation
agents copied to clipboard

MCP unstable_getAITools doesn't seem to work when there are no args

Open kchro3 opened this issue 7 months ago • 1 comments

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

kchro3 avatar Jun 20 '25 01:06 kchro3

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 🤷

p0tr3c avatar Jun 22 '25 09:06 p0tr3c