Skip to content

feat(api): let an MCP tool return only the fields an agent names - #3540

Merged
fforootd merged 26 commits into
mainfrom
mcp-tool-fields
Dec 2, 2026
Merged

fforootd merged 26 commits into
mainfrom
mcp-tool-fields

Conversation

@fforootd

@fforootd fforootd commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Context

Summary

  • MCP tools whose operation answers a resource, or a page of them (an object with next_page_token and one list), take an optional fields argument: dot-paths, as the CLI's --fields takes them (apps/cli/src/cli/core/columns.ts). 66 of the 79 tools take it. The 10 that answer no JSON body do not, and neither do the three whose answer holds a credential shown once (publishApplication, createInvitation, createProjectClaim, marked x-zitadel-returns-secret): a path that cut the credential out would lose it, so they refuse fields as an unknown argument.
  • The endpoint calls the API exactly as before and cuts the answer afterwards (internal/mcp/fields.go). Each resource keeps the named paths, nested as they were; a page keeps next_page_token. Values are kept as the API wrote them (numbers as written, no HTML escaping). A refusal and an empty answer (createGrant's 202) come back as without fields. The API contract and its authorization do not change.
  • A path that matches nothing: left out of the resource that lacks it, as the CLI leaves it out. When a path matched nothing in a non-empty answer, a second text block names it, so a misspelt path (occured_at) does not read as data the project lacks. The answer is not judged against the response schema, whose open objects (an event's payload) cannot say which paths exist. A fields value that is not a list of dot-paths (including null) is refused before any request; an operation that answers no resource refuses fields as any unknown argument, before anyone is asked to confirm.
  • No operation declares a parameter called fields (checked against the bundled spec); TestNoOperationTakesAParameterCalledFields and inputSchema keep it that way.
  • The argument is declared bare ({"type":"array","items":{"type":"string"}}) on each tool and explained once in the server's instructions.
  • A call names at most 64 paths of at most 256 bytes each; paths are split and ordered once per call. The question before a destructive call does not show fields, which only cuts the answer (a long path could otherwise push project_id out of the question's 600-byte cut).

Validation

  • go test -race -count=1 ./internal/mcp/ (new tests: catalog shape detection, page and resource cuts, unmatched-path note, empty page, refusal and empty answer untouched, malformed, null and over-long fields, the destructive-call question without fields, fields on an operation without a resource)
  • On the head merged with main (192a2dd): go vet and go test -count=1 for ./internal/mcp/, ./internal/api/ and ./cmd/server/, the whole go test -tags sqlite_integration ./internal/api/integration_test/, turbo run generate for @zitadel/server and @zitadel/api (no drift), pnpm run check-openapi-rules, scripts/go-lint.mjs
  • Earlier, on the stacked head: go vet ./..., go test -count=1 ./internal/mcp/, go test -count=1 -tags sqlite_integration ./internal/api/integration_test/ -run TestMCP
  • Earlier on main: go test -count=1 ./... in apps/api (after building the console, which cmd/server embeds), gofmt -l, go run ./tools/analyzers/cmd/analyzers ./internal/mcp/...
  • node scripts/check-pr-title.mjs, node scripts/check-changesets-status.mjs --pending

Release notes / changeset

Notes

https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy


Generated by Claude Code

A tool whose operation answers a resource, or a page of them, takes
`fields`: dot-paths, as the CLI's `--fields` takes them. The endpoint asks
the API as always and cuts the answer, so each resource keeps those paths
and a page keeps next_page_token. A path that matched nothing in a
non-empty answer is named beside the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy
The OAuth endpoints authenticate no bearer, so the events they write took an
empty client_id from the request and named the client only in the payload.
A filter on client_id then missed the tokens a client was issued. EmitSpec
takes the client, and auth.request.created, auth.request.exchange.failed and
the auth.token.issued and auth.token.revoked of a code or refresh grant set it.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
The MCP endpoint shows only the first sentence of an operation's description,
and agents guessed wrong from it: search without project_id read only a
person's newest projects, listProjectMembers left out platform operators, and
listEvents said nothing about how to read one sign-in. An operation may now
say the rest in x-zitadel-agent-note, which the tool's description adds; the
list's budget rises to 132,000 bytes for it. The server's instructions no
longer say that a project_id left out is always the credential's project
without saying what that is for a person.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
Copilot AI balanced review requested due to automatic review settings December 2, 2026 00:57
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cloud Ready Ready Preview Dec 2, 2026 6:26am UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-12-02T01:07:23.850771Z a827868 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

🦋 Changeset detected

Latest commit: 192a2dd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@zitadel/server Minor
@zitadel/cli Minor
@zitadel/testing Minor
@zitadel/server-darwin-arm64 Minor
@zitadel/server-darwin-x64 Minor
@zitadel/server-linux-arm64 Minor
@zitadel/server-linux-x64 Minor
@zitadel/server-win32-x64 Minor
@zitadel/api Minor
@zitadel/components Minor
@zitadel/config Minor
@zitadel/sdk-angular Minor
@zitadel/sdk-core Minor
@zitadel/sdk-next Minor
@zitadel/sdk-nuxt Minor
@zitadel/sdk-qwik Minor
@zitadel/sdk-qwik-router Minor
@zitadel/sdk-react Minor
@zitadel/sdk-solid Minor
@zitadel/sdk-solid-start Minor
@zitadel/sdk-svelte Minor
@zitadel/sdk-sveltekit Minor
@zitadel/sdk-tanstack-start Minor
@zitadel/sdk-vue Minor
@zitadel/cloud Patch
@zitadel/client-sdk-contract Patch
@zitadel/server-sdk-contract Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active issues cause empty 202 responses to lose their acknowledgement and status, while explicit fields: null bypasses validation and returns the full response.

2 open issues
What changed in this PR

Adds optional field selection to MCP tools, reducing agent context usage without changing API requests or authorization.

Changes:

  • Detects resource and page responses and exposes a fields argument.
  • Filters successful responses and reports unmatched paths.
  • Adds tests, documents the contract, and adjusts the tool-list size budget.
File Description
docs/​adrs/​092-mcp-server.md Documents field selection and the revised size budget.
apps/​api/​internal/​mcp/​handler.go Integrates field selection into tool calls.
apps/​api/​internal/​mcp/​handler_test.go Tests filtering, validation, and unchanged refusals.
apps/​api/​internal/​mcp/​fields.go Implements path validation and response filtering.
apps/​api/​internal/​mcp/​catalog.go Detects response shapes and declares fields.
apps/​api/​internal/​mcp/​catalog_test.go Tests catalog behavior and updates the size budget.
.changeset/​mcp-endpoint.md Adds field selection to release notes.

Review effort: Balanced

Comment thread apps/api/internal/mcp/fields.go Resolved
Comment thread apps/api/internal/mcp/handler.go Resolved
…ential

The MCP endpoint offered no operation whose answer holds a credential, so an
agent could not register an application, invite a person or start a
project's claim. Zitadel is to be driven by agents, with the caller's
credential deciding what it may do, so publishApplication, createInvitation
and createProjectClaim become tools. x-zitadel-returns-secret now words the
tool: its description says the answer holds a credential, to pass to the
user and write nowhere else. The three such operations an application calls
while a user signs in stay out by their plane. The list's budget rises to
142,000 bytes for the three tools.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…ng changesets

The MCP endpoint and refresh tokens have not been released, so what this
branch changes about them belongs in their pending changesets
(.changeset/README.md). The authorization and token endpoints have been, so
the client on their events keeps a patch changeset of its own.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…into claude/mcp-secret-answers

# Conflicts:
#	.changeset/mcp-endpoint.md
Prepared to stack #3540 on #3542 once the user agrees. listBudget stays
142,000: 79 tools weigh 141,620 bytes, 69 of them with fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy
… a credential

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy
… three tools

A review found the ADR's claim wrong that a client secret a tool returns is
ended by rotating it: only a retry with the request's Idempotency-Key
replaces a secret, and a tool sends no header. The ADR now says what ends
each credential, the operator's reference no longer says such an operation
is never a tool, and the amendment no longer names a guard that only
#3543 adds. createInvitation and createProjectClaim are now
called through their tools against the real API.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…rop the credential hint

- A path takes at most 256 bytes; paths are split and ordered once per call,
  not once per resource.
- The question before a destructive call no longer shows fields, which only
  cuts the answer and could push project_id out of the 600-byte cut.
- The instructions no longer suggest leaving out a credential a tool
  answers with: a show-once secret cut that way is lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy
…in full

A review found two places where the MCP tools still misled. Every tool
described project_id as defaulting to the credential's project, also on
search, which declares its own optional one and reads a person's projects
when it is left out: an operation's own optional project_id now carries
that operation's words. And the descriptions of listEvents and
listProjectMembers did not say what their agent notes say, though the
OpenAPI rules promise they do: both now say it.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…into claude/mcp-secret-answers

# Conflicts:
#	docs/adrs/092-mcp-server.md
…l/nextgen into mcp-tool-fields

# Conflicts:
#	docs/adrs/092-mcp-server.md
…s are kept

The note on a tool that answers with a credential told an agent to pass it to
the user and write it nowhere else. A coding agent's proper place for a client
secret is the store the user keeps secrets in, such as the app's env file, so
the note now allows that.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
A credential publishApplication, createInvitation or createProjectClaim
answers with is shown once; a path that cut it out would lose it. Their
tools no longer declare fields and refuse it as an unknown argument. The
list is 156 bytes lighter: 79 tools weigh 141,497 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmRRBTn7WmrnkFEFMDy
… question

The endpoint asks the person at the client before a destructive call and
refuses the call from a client that cannot ask. cancelInvitation is what
ends an invitation's link, which createInvitation now hands to an agent, so
a client on an older protocol revision could not end one that leaked.

An operation may now say x-zitadel-unasked: the endpoint sends its call as
it is. Its tool keeps the protocol's destructive hint, so a client can ask
its own user, and no longer says the user is asked. cancelInvitation is the
one operation marked, and a test names the marked ones, since each is a
decision. The CLI is unchanged and still asks.

ADR 092 also records that a client secret is to be replaced through an
operation of its own, in a later change.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
Picks up #3545, which the base branch did not hold.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
#3537 landed on main as one commit. This branch already holds its changes
through the old base branch, so the merge takes nothing from it: it only
records main as an ancestor, so that the pull request shows its own change.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…n is promised

The ADR index and ADR 092's summary still said every destructive call is
confirmed. The OpenAPI rules read as if any removal that ends access should
be marked x-zitadel-unasked; they now say only cancelInvitation is, by
ADR 092 §6, and name the test that holds it.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
Base automatically changed from claude/mcp-secret-answers to main December 2, 2026 05:46
Takes x-zitadel-unasked and operation.asks() beside the fields argument:
the catalog keeps both new fields, the server's instructions keep both
sentences, and the changeset says both. 79 tools weigh 141,563 bytes, so
ADR 092 §3's combined figure stands.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
Picks up what landed on main in between: #3538, #3541, #3539 and #3543.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
#3542 landed on main as one commit. This branch already holds its changes
through the old base branch, so the merge takes nothing from it: it only
records main as an ancestor, so that the pull request shows its own change.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
Picks up #3546 and #3547, which landed after #3542.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
@fforootd
fforootd enabled auto-merge December 2, 2026 06:24
@fforootd
fforootd added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit ebe0e1a Dec 2, 2026
18 checks passed
@fforootd
fforootd deleted the mcp-tool-fields branch December 2, 2026 06:32
@github-project-automation github-project-automation Bot moved this from Inbox to Done in Engineering Kanban Dec 2, 2026

This branch was successfully deployed

1 active deployment
Preview — 192a2dda Deployed Dec 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants