ai icon indicating copy to clipboard operation
ai copied to clipboard

Add support for MCP tool annotations

Open raviships opened this issue 1 year ago • 2 comments

Problem

The annotation hints are lost when getting the tools from the experimental_createMCPClient.

Use case

I want to be able to require execution confirmation for some of the tools from my MCP.

Details

I'm using the experimental_createMCPClient to get the tools from my MCP and pass it into the ai-sdk. This part is working great! But some of the tools should need user confirmation before being executed by the LLM.

As far as I know, the official way to handle this is through tool annotation hints. (e.g. { destructiveHint: true })

I was planning to use the hints to manually remove the tool's execute parameter, so that it becomes a HITL tool (as per the HITL cookbook). But then I realized it wasn't possible because the hints are lost when I get the tools.

Please let me know if there is a workaround to achieve this.

Thanks in advance!

AI SDK Version

ai: 4.3.16

raviships avatar Jun 13 '25 07:06 raviships

Identify issue type

Identified type: Feature
Confidence: High

Rationale: The issue requests support for preserving/exposing MCP tool annotation hints as an additional capability. It is not framed as a violation of documented AI SDK behavior, so it is best classified as a feature request.

Evidence:

  • Title asks to “Add support for MCP tool annotations.”
  • Description says annotation hints such as destructiveHint are lost when tools are obtained from experimental_createMCPClient.
  • Reporter wants to use those hints to require user confirmation / human-in-the-loop execution for MCP tools and asks for a workaround.

lgrammel avatar Jul 02 '26 09:07 lgrammel

Opened #16752 to implement this.

The earlier attempt (#10463) was closed on design grounds — @lgrammel noted it "bleeds the MCP spec into our tool spec" by adding an MCP-shaped annotations field onto the core Tool type, suggested a generic metadata/hints property instead, and asked for tests + docs; @nicoalbanese "feel[s] strongly that this would be best implemented in some kind of generic tool metadata."

This PR is built around that feedback:

  • No changes to the core Tool type. @ai-sdk/provider-utils, @ai-sdk/provider, and ai are untouched — all production changes are confined to @ai-sdk/mcp. The MCP spec never reaches the abstract tool spec.
  • Rides the existing generic metadata container — the "generic tool metadata" both of you pointed at. The core Tool already exposes metadata?: JSONObject (which propagates to the tool call's toolMetadata); the MCP client just writes the hints into it under a namespaced annotations key. MCP's vocabulary is typed only inside @ai-sdk/mcp.
  • Namespaced, not flattened, so the hints can't collide with sibling metadata keys and stay self-evidently MCP-sourced.
  • All four hints pass through, but only as generic MCP-local metadata — so there's no cost to including destructive/idempotent/openWorld (and destructiveHint is the one this issue was really about, for confirmation-gating).
  • Cast-free consumer ergonomics + correct scoping via two small helpers: isMCPToolCall(source) to detect MCP tools and scope logic (so a generic toolApproval doesn't accidentally gate non-MCP tools), and getMCPToolAnnotations(source) to read the typed hints without casting.
  • Tests, docs, and a runnable example included.

Usage — gating execution on the hints via toolApproval:

import { getMCPToolAnnotations, isMCPToolCall } from '@ai-sdk/mcp';

await generateText({
  model,
  tools: await client.tools(),
  toolApproval: ({ toolCall }) => {
    if (isMCPToolCall(toolCall)) {
      const a = getMCPToolAnnotations(toolCall);
      if (a?.readOnlyHint === true) return 'approved';
      if (a?.destructiveHint === true) return 'user-approval';
      return 'user-approval';
    }
    // your own policy for non-MCP tools
    return 'approved';
  },
  prompt: '…',
});

Feedback on the design welcome before it's reviewed in depth.

manupadev-taxxa avatar Jul 06 '26 11:07 manupadev-taxxa