mcptools icon indicating copy to clipboard operation
mcptools copied to clipboard

tool_as_json() omits annotations field from tools/list response

Open mrcanofcatfood opened this issue 6 months ago • 1 comments

Problem

tool_as_json() in R/server.R serializes tool definitions for the tools/list MCP response, but only includes name, description, and inputSchema — it omits the annotations field entirely.

The MCP specification (2025-03-26+) defines tool annotations as part of each tool's definition in tools/list. ellmer already supports declaring them via tool(annotations = tool_annotations(...)) and stores them on the ToolDef S7 object. mcptools just doesn't include them in the JSON response.

Current Code

# R/server.R, line 479
tool_as_json <- function(tool) {
  # ... inputSchema computation ...
  list(
    name = tool@name,
    description = tool@description,
    inputSchema = inputSchema
    # annotations: missing
  )
}

Expected MCP Response

{
  "name": "draw_random",
  "description": "Draw random numbers",
  "inputSchema": { ... },
  "annotations": {
    "readOnlyHint": true,
    "idempotentHint": true,
    "openWorldHint": false
  }
}

Fix

One line addition in tool_as_json():

list(
  name = tool@name,
  description = tool@description,
  inputSchema = inputSchema,
  annotations = if (length(tool@annotations)) tool@annotations else NULL
)

This is backwards-compatible — tools without annotations get annotations: null (or omitted if the client filters nulls), which is the same as current behaviour.

Use Case

We're building an SDM (species distribution modelling) MCP server with ~70 tools. Some are read-only queries, some are destructive (delete project), some make network calls. Tool annotations let MCP clients show appropriate confirmation prompts and enforce safety — but they're silently dropped today.

Happy to open a PR for this.

Environment

  • mcptools 0.2.1
  • ellmer 0.3.x

mrcanofcatfood avatar May 20 '26 11:05 mrcanofcatfood

This seems worth covering at the tools/list boundary, not only in tool_as_json(), because the safety impact shows up in client approval prompts.

Regression checks I would add:

  • A read-only tool declared with tool_annotations(readOnlyHint = TRUE, idempotentHint = TRUE, openWorldHint = FALSE) preserves those fields in the tools/list response.
  • A mutating example such as delete_project preserves destructiveHint and openWorldHint, so clients can show a stronger confirmation prompt.
  • An unannotated tool has an explicit expected shape: either omit annotations or emit annotations = NULL, but keep that behavior covered.
  • A snapshot-style test exercises the full tools/list response, since that is where MCP clients consume the metadata.

For a quick outside check, I added annotation findings to a small free metadata checker:

npx --yes --package github:kayalopez/ai-agent-launch-tools#v0.1.13 mcp-permission-matrix --file tools-list.json --json

It reads a redacted tools/list export only and flags cases like missing_tool_annotations and missing_destructive_hint without invoking tools. Release notes: https://github.com/kayalopez/ai-agent-launch-tools/releases/tag/v0.1.13

kayalopez avatar May 24 '26 18:05 kayalopez