From c9c6a17e8db928642f3ec8b717789bbef8886a52 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 07:27:47 +0000 Subject: [PATCH 1/2] feat: connect a coding agent to the server's MCP endpoint from zitadel setup setup writes a root .mcp.json, the file Claude Code and the Copilot agent in VS Code read, naming the server's MCP endpoint as the server `zitadel`. The file is committed and holds no secret: its headersHelper, which Claude Code runs in the project's directory once the person trusts the folder, reads the project secret from the git-ignored .zitadel/secret, and its headers name ${ZITADEL_PROJECT_SECRET:-} for a client that fills it from its environment. setup adds its server beside the servers a .mcp.json already names and leaves alone one that names `zitadel` or is no JSON object. The AGENTS.md guidance names the endpoint, a docs guide explains the file and how to connect other clients, and ADR 092 records the answer to its open question 1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0137eGmSarR3gn9oz3Btwd8H --- .changeset/setup-mcp-client-config.md | 5 + apps/cli/skills/zitadel-cli/SKILL.md | 14 ++ apps/cli/src/lib/json.ts | 45 +++++- apps/cli/src/lib/orca/patchers/rule/base.ts | 6 +- .../src/lib/orca/patchers/rule/guidance.ts | 9 +- .../src/lib/orca/patchers/rule/mcp-client.ts | 136 ++++++++++++++++++ apps/cli/tests/helpers/project.ts | 10 ++ apps/cli/tests/integration/setup.spec.ts | 45 ++++++ apps/cli/tests/unit/lib/json.test.ts | 34 +++++ .../lib/orca/patchers/rule/guidance.test.ts | 11 ++ .../lib/orca/patchers/rule/mcp-client.test.ts | 120 ++++++++++++++++ .../patchers/rule/qwik-router/index.test.ts | 4 +- .../patchers/rule/sveltekit/index.test.ts | 4 +- .../rule/tanstack-start/index.test.ts | 4 +- apps/docs/content/docs/get-started/agents.mdx | 4 + .../content/docs/guides/connect-an-agent.mdx | 69 +++++++++ apps/docs/content/docs/guides/meta.json | 1 + apps/docs/content/docs/sdks/angular.mdx | 2 +- apps/docs/content/docs/sdks/next.mdx | 2 +- apps/docs/content/docs/sdks/nuxt.mdx | 2 +- apps/docs/content/docs/sdks/qwik-router.mdx | 2 +- apps/docs/content/docs/sdks/qwik.mdx | 2 +- apps/docs/content/docs/sdks/react.mdx | 2 +- apps/docs/content/docs/sdks/solid-start.mdx | 2 +- apps/docs/content/docs/sdks/solid.mdx | 2 +- apps/docs/content/docs/sdks/svelte.mdx | 2 +- apps/docs/content/docs/sdks/sveltekit.mdx | 2 +- .../docs/content/docs/sdks/tanstack-start.mdx | 2 +- apps/docs/content/docs/sdks/vue.mdx | 2 +- docs/adrs/092-mcp-server.md | 15 +- 30 files changed, 534 insertions(+), 26 deletions(-) create mode 100644 .changeset/setup-mcp-client-config.md create mode 100644 apps/cli/src/lib/orca/patchers/rule/mcp-client.ts create mode 100644 apps/cli/tests/unit/lib/orca/patchers/rule/mcp-client.test.ts create mode 100644 apps/docs/content/docs/guides/connect-an-agent.mdx diff --git a/.changeset/setup-mcp-client-config.md b/.changeset/setup-mcp-client-config.md new file mode 100644 index 0000000000..545158e236 --- /dev/null +++ b/.changeset/setup-mcp-client-config.md @@ -0,0 +1,5 @@ +--- +"@zitadel/cli": minor +--- + +`zitadel setup` writes `.mcp.json` at the app's root, so Claude Code and the Copilot agent in VS Code started in the app are connected to the server's MCP endpoint (`/api/v1/mcp`) without a further step. The committed file holds no secret: Claude Code reads the project secret from the git-ignored `.zitadel/secret` through the file's `headersHelper`, and VS Code fills `${ZITADEL_PROJECT_SECRET}` from its environment. Setup adds its server beside the servers a `.mcp.json` already names, and leaves alone one that already names `zitadel`. diff --git a/apps/cli/skills/zitadel-cli/SKILL.md b/apps/cli/skills/zitadel-cli/SKILL.md index 88b2b034ab..bbe6f21d9a 100644 --- a/apps/cli/skills/zitadel-cli/SKILL.md +++ b/apps/cli/skills/zitadel-cli/SKILL.md @@ -854,6 +854,20 @@ is not ignored, and warns when `.env.example` is, as an older setup left SvelteKit, Nuxt and SolidStart apps; `doctor --fix` appends `!.env.example` while the file names variables without values. +`setup` also writes `.mcp.json` at the app's root, the file Claude Code and +the Copilot agent in VS Code read: the server `zitadel` at the server's MCP +endpoint, `/api/v1/mcp`, whose tools are the API's operations. The +file is committed and holds no secret: its `headersHelper` reads the project +secret from `.zitadel/secret` for Claude Code, and its `headers` name +`${ZITADEL_PROJECT_SECRET}` for a client that fills it from its environment. +Setup adds its server to a `.mcp.json` that names others, keeping their entries, +and leaves alone one that already names `zitadel` or is not a JSON object, so +`data.files_written` lists the file only when setup changed it. It writes +none for `--server mock`. A session the developer starts in the app later +sees the endpoint's tools once they approve the server. You have a shell, so +keep using the CLI: `api ` calls the same operations and its +output costs your context less. + In an app that already exists, `setup` adds the sign-in, sign-up and profile pages and the protection for the pages it guards, and leaves the app's own pages as they are. Read the app's pages afterwards and add what the developer diff --git a/apps/cli/src/lib/json.ts b/apps/cli/src/lib/json.ts index 5f47a65311..7b344795c6 100644 --- a/apps/cli/src/lib/json.ts +++ b/apps/cli/src/lib/json.ts @@ -33,11 +33,34 @@ export function setTopLevelJsonKey( path: string, key: string, value: unknown, +): string { + return setJsonValue(source, path, [key], value); +} + +/** + * {@link setTopLevelJsonKey} for a value under nested keys: `["mcpServers", + * "zitadel"]` sets `zitadel` inside the top-level `mcpServers`, creating + * `mcpServers` when the document has none. Every key above the last must name + * an object or nothing, or the edit throws. + * + * @param source - The document's text. + * @param path - The file's name, for the error. + * @param keys - The keys to the value, outermost first; at least one. + * @param value - The value to set. + * @returns The document's text with the value set. + * @throws {Error} When the document is no JSON object, or the edit does not + * verify. + */ +export function setJsonValue( + source: string, + path: string, + keys: readonly [string, ...string[]], + value: unknown, ): string { const parsed = parseJsonObject(source, path); const indent = detectIndent(source); const compact = indent === 0; - const edits = modify(source, [key], value, { + const edits = modify(source, [...keys], value, { // A compact document gets no formatting, so the value goes in on one line. formattingOptions: compact ? undefined @@ -57,13 +80,29 @@ export function setTopLevelJsonKey( const next = applyEdits(source, edits); const verification = parseJsonObject(next, path); - const expected = { ...parsed, [key]: value }; - if (JSON.stringify(verification) !== JSON.stringify(expected)) { + if (JSON.stringify(verification) !== JSON.stringify(withValueAt(parsed, keys, value))) { throw new Error(`refusing to write ${path}: the targeted JSON edit did not verify`); } return next; } +/** + * @param target - The value to set the value in. It is not changed. + * @param keys - The keys to the value, outermost first. + * @param value - The value. + * @returns `value` when no key is left, else a copy of `target` with `value` + * at `keys`, every object on the way copied and a missing one created, as + * `modify` does it. + */ +function withValueAt(target: unknown, keys: readonly string[], value: unknown): unknown { + const [key, ...rest] = keys; + if (key === undefined) { + return value; + } + const object = isObject(target) ? target : {}; + return { ...object, [key]: withValueAt(object[key], rest, value) }; +} + /** * Infers the indentation unit of a JSON document: the whitespace prefix of * its first indented line, a compact document stays compact (`0`), and diff --git a/apps/cli/src/lib/orca/patchers/rule/base.ts b/apps/cli/src/lib/orca/patchers/rule/base.ts index 6f3be0ee47..1757e67412 100644 --- a/apps/cli/src/lib/orca/patchers/rule/base.ts +++ b/apps/cli/src/lib/orca/patchers/rule/base.ts @@ -28,6 +28,7 @@ import type { PatchResult, PatchView, } from "@/lib/orca/patchers/types"; +import { mcpClientOps } from "./mcp-client"; import { reclaimableOps, withoutExistingTargets } from "./reclaim"; import { Project } from "@/lib/project"; @@ -317,8 +318,8 @@ export abstract class AbstractRulePatcher implements Patcher { /** * The framework-agnostic `.zitadel/` base files every rule patcher writes: - * the project secret, `zitadel.json`, env templates, and an empty sync - * state. The `schemas/` and `flows/` directories are created empty here; + * the project secret, `zitadel.json`, env templates, an empty sync state, + * the guidance sections and the MCP client's `.mcp.json`. The `schemas/` and `flows/` directories are created empty here; * setup fills them from versioned local defaults after patching. * Pure: no filesystem or network. */ @@ -392,6 +393,7 @@ export abstract class AbstractRulePatcher implements Patcher { contents: `${stableStringify({ framework: ctx.framework.id })}\n`, }, ...guidanceOps(ctx), + ...mcpClientOps(ctx.server), ]; } diff --git a/apps/cli/src/lib/orca/patchers/rule/guidance.ts b/apps/cli/src/lib/orca/patchers/rule/guidance.ts index 2685d899f7..e0e0444f62 100644 --- a/apps/cli/src/lib/orca/patchers/rule/guidance.ts +++ b/apps/cli/src/lib/orca/patchers/rule/guidance.ts @@ -8,6 +8,7 @@ */ import type { PatchContext } from "@/lib/orca/patchers/types"; import { Spelling } from "@/lib/suggestion"; +import { MCP_CONFIG_FILE, MCP_SERVER_NAME, mcpEndpointOf } from "./mcp-client"; const MARKER_BEGIN = ""; const MARKER_END = ""; @@ -97,6 +98,12 @@ export function agentsGuidanceSection(ctx: PatchContext): string { // app that protects a checkout cannot tell whether the visitor returns to it. const returnPathParagraph = "After sign-in the login goes to the path in its page's `?next=` parameter, ahead of `post-sign-in-url`, so a visitor a protected route sent to sign in lands back on that route; a page of your own can read `next` to tell them why they must sign in. Set `force-post-sign-in-url` (SDK wrappers: `forcePostSignInUrl`) on a login whose page uses `next` for something else."; + // The endpoint is the server's, so a project on the mock server has none. + const endpoint = mcpEndpointOf(ctx.server); + const mcpParagraph = + endpoint === undefined + ? "" + : `\n\nAn MCP client started in this directory is connected to the project through \`${MCP_CONFIG_FILE}\`, as the server \`${MCP_SERVER_NAME}\`: the server's endpoint ${endpoint}, which offers each operation of the API as a tool and asks the person before a call that deletes, revokes, rolls back or deploys. Claude Code reads the project secret from \`.zitadel/secret\` through the file's \`headersHelper\`, once you trust the folder; the Copilot agent in VS Code sends \`ZITADEL_PROJECT_SECRET\` from the environment it was started in. With a shell, the CLI does the same work and costs the context less.`; return `## Authentication (Zitadel) This app's login is managed by Zitadel. Local config is the source of truth for what the login collects and how sign-in works (step 3 below). The app files setup generated are yours to change, from the pages to the request boundary that protects routes in a server-rendered app: \`doctor\` reports a file you changed as \`edited\`, or as \`adopted\` once you delete its \`zitadel-cli: managed-file\` comment, and \`doctor --fix\` restores only a missing file, never one you changed. @@ -120,7 +127,7 @@ ${themingParagraph} ${sessionStateParagraph} -${returnPathParagraph} +${returnPathParagraph}${mcpParagraph} Machine-readable dialect (read these before authoring flow or schema edits): diff --git a/apps/cli/src/lib/orca/patchers/rule/mcp-client.ts b/apps/cli/src/lib/orca/patchers/rule/mcp-client.ts new file mode 100644 index 0000000000..827e6ed6b0 --- /dev/null +++ b/apps/cli/src/lib/orca/patchers/rule/mcp-client.ts @@ -0,0 +1,136 @@ +/** + * The MCP client configuration setup writes beside the app (ADR 092, open + * question 1): a root `.mcp.json`, the file Claude Code and the Copilot agent + * in VS Code read, naming the server's MCP endpoint, so that a coding agent + * started in the project is connected to the project with no step of its own. + * + * The file is committed, so it holds no secret. Claude Code takes the bearer + * from `headersHelper`, a command it runs in the project's directory once the + * person trusted the folder, which reads the project secret from the + * git-ignored `.zitadel/secret`. VS Code runs no helper: it sends `headers`, + * whose `${ZITADEL_PROJECT_SECRET:-}` it fills from the environment it was + * started in. Where both apply, the helper's header replaces the static one. + * + * A configuration someone wrote stays as it is: setup adds its server to a + * file that names no server of that name, and leaves alone a file it cannot + * read as JSON. + */ +import { apiBaseUrl } from "@/lib/backend/transport"; +import { isObject, parseJsonOrUndefined, setJsonValue } from "@/lib/json"; +import type { FileOp } from "./file-writer/types"; + +/** The file setup writes, at the project's root. */ +export const MCP_CONFIG_FILE = ".mcp.json"; + +/** The name the endpoint is configured under: a client's tools are `mcp__zitadel__…`. */ +export const MCP_SERVER_NAME = "zitadel"; + +/** + * The command Claude Code runs for the endpoint's headers: it prints the + * project secret of `.zitadel/secret` as a bearer, as the JSON object of + * headers Claude Code reads from it. Node is there in every app setup + * supports, and it runs in the directory `.mcp.json` is in. + */ +export const MCP_HEADERS_HELPER = + "node -p \"JSON.stringify({Authorization:'Bearer '+JSON.parse(require('fs').readFileSync('.zitadel/secret','utf8')).project_secret})\""; + +/** + * The header a client that runs no helper sends: the project secret, from the + * environment it was started in. The CLI and the app read the same variable. + * Its empty default keeps Claude Code, whose helper's header replaces this + * one, from warning about a variable it does not need. + */ +const STATIC_AUTHORIZATION = "Bearer ${ZITADEL_PROJECT_SECRET:-}"; + +/** The server entry setup writes, in the order a person reads it. */ +type McpServerEntry = Readonly<{ + type: "http"; + url: string; + headers: Readonly<{ Authorization: string }>; + headersHelper: string; +}>; + +/** + * @param server - The server setup created the project on. + * @returns The endpoint's URL on it, or `undefined` for a server that is not + * one (`mock`), which has no endpoint to connect to. + */ +export function mcpEndpointOf(server: string): string | undefined { + if (!URL.canParse(server)) { + return undefined; + } + const { protocol } = new URL(server); + if (protocol !== "http:" && protocol !== "https:") { + return undefined; + } + return `${apiBaseUrl(server)}/mcp`; +} + +/** + * @param url - The endpoint's URL. + * @returns The entry that connects a client to it. + */ +export function mcpServerEntry(url: string): McpServerEntry { + return { + type: "http", + url, + headers: { Authorization: STATIC_AUTHORIZATION }, + headersHelper: MCP_HEADERS_HELPER, + }; +} + +/** + * Adds the endpoint to an MCP client's configuration, leaving everything the + * file already says as it is. + * + * @remarks + * Only a missing entry is added: a file that already names a server + * `zitadel`, whatever it says, was written by someone, perhaps setup on an + * earlier run, and is theirs. A file that is no JSON object, or whose + * `mcpServers` is no object, is left as it is too: setup does not rewrite what + * it cannot read. Pure and idempotent, so the file-writer writes nothing when + * the answer is the source. + * + * @param source - The file's text, or `undefined` when there is none. + * @param url - The endpoint's URL. + * @returns The file's text with the endpoint configured. + */ +export function upsertMcpServer(source: string | undefined, url: string): string { + const entry = mcpServerEntry(url); + if (source === undefined || source.trim() === "") { + return `${JSON.stringify({ mcpServers: { [MCP_SERVER_NAME]: entry } }, null, 2)}\n`; + } + const parsed = parseJsonOrUndefined(source); + if (!isObject(parsed)) { + return source; + } + const servers = parsed.mcpServers; + if (servers !== undefined && !isObject(servers)) { + return source; + } + if (servers !== undefined && MCP_SERVER_NAME in servers) { + return source; + } + return setJsonValue(source, MCP_CONFIG_FILE, ["mcpServers", MCP_SERVER_NAME], entry); +} + +/** + * @param server - The server setup created the project on. + * @returns The edit that configures the endpoint for an MCP client, or none + * for a server without one. + */ +export function mcpClientOps(server: string): FileOp[] { + const url = mcpEndpointOf(server); + if (url === undefined) { + return []; + } + return [ + { + kind: "edit", + path: MCP_CONFIG_FILE, + edit: (source) => { + return upsertMcpServer(source, url); + }, + }, + ]; +} diff --git a/apps/cli/tests/helpers/project.ts b/apps/cli/tests/helpers/project.ts index bb1c6c1247..cf49852a93 100644 --- a/apps/cli/tests/helpers/project.ts +++ b/apps/cli/tests/helpers/project.ts @@ -359,6 +359,11 @@ class CommittedFiles { return this.read(".zitadel/branding/branding.json"); } + /** The MCP client's configuration, as the developer commits it, as text. */ + mcpConfig(): Promise { + return readFile(join(this.root, ".mcp.json"), "utf8"); + } + /** Every committed configuration document as one string. */ async asText(slug: string): Promise { const documents = await Promise.all([ @@ -2309,6 +2314,11 @@ export class ScaffoldedApp { return this.hasProjectFile("zitadel.json"); } + /** Writes the MCP client's configuration, as a developer who configured one by hand would. */ + writeMcpConfig(contents: string): Promise { + return this.writeProjectFile(".mcp.json", contents); + } + /** Writes `zitadel.json`, for a spec arranging a particular configuration. */ writeConfig(config: Record): Promise { return this.writeProjectFile("zitadel.json", JSON.stringify(config, null, 2)); diff --git a/apps/cli/tests/integration/setup.spec.ts b/apps/cli/tests/integration/setup.spec.ts index b3c5dbb32a..d6e1afdfbd 100644 --- a/apps/cli/tests/integration/setup.spec.ts +++ b/apps/cli/tests/integration/setup.spec.ts @@ -170,6 +170,17 @@ function brandingStep(app: { readonly path: string }): string { return `Change how the login looks: it is a component in your app, so theme it with --zl-* custom properties in your own stylesheet and build any page layout around it in your code. To change the widget's structure, ${expectedPublicCliCommand(`branding eject --cwd ${app.path}`)} scaffolds an editable template in .zitadel/branding/.`; } +/** + * An MCP client configuration a developer wrote before setup ran, which names + * the server setup would configure, pointed elsewhere. + */ +const HAND_WRITTEN_MCP_CONFIG = `{ + "mcpServers": { + "zitadel": { "type": "http", "url": "https://zitadel.example.com/api/v1/mcp" } + } +} +`; + /** * Every file setup writes into an app, in the order it reports them. */ @@ -183,6 +194,7 @@ const FILES_WRITTEN = [ ".zitadel/state.json", "AGENTS.md", "README.md", + ".mcp.json", "app/login/page.tsx", "app/register/page.tsx", "app/profile/page.tsx", @@ -232,6 +244,7 @@ const FILES_WOULD_WRITE = [ ".zitadel/state.json", "AGENTS.md", "README.md", + ".mcp.json", "app/login/page.tsx", "app/register/page.tsx", "app/profile/page.tsx", @@ -270,6 +283,7 @@ const FILES = [ { path: ".zitadel/state.json", kind: "file", action: "create" }, { path: "AGENTS.md", kind: "file", action: "create" }, { path: "README.md", kind: "file", action: "create" }, + { path: ".mcp.json", kind: "file", action: "create" }, { path: "app/login/page.tsx", kind: "file", action: "create" }, { path: "app/register/page.tsx", kind: "file", action: "create" }, { path: "app/profile/page.tsx", kind: "file", action: "create" }, @@ -306,6 +320,7 @@ const NEW_APP_FILES_WRITTEN = [ ".zitadel/state.json", "AGENTS.md", "README.md", + ".mcp.json", "app/page.tsx", "app/login/page.tsx", "app/register/page.tsx", @@ -355,6 +370,7 @@ const NEW_APP_FILES = [ { path: ".zitadel/state.json", kind: "file", action: "create" }, { path: "AGENTS.md", kind: "file", action: "create" }, { path: "README.md", kind: "file", action: "create" }, + { path: ".mcp.json", kind: "file", action: "create" }, { path: "app/page.tsx", kind: "file", action: "update" }, { path: "app/login/page.tsx", kind: "file", action: "create" }, { path: "app/register/page.tsx", kind: "file", action: "create" }, @@ -669,6 +685,35 @@ describe("setup", () => { expect(data.diff?.files).toEqual([]); }); + describe("for an MCP client", () => { + it("commits the configuration that connects one", async () => { + const app = await anApp(); + + await app.setup(); + + expect(await app.trackedFiles()).toContain(".mcp.json"); + }); + + it("keeps the project secret out of that configuration", async () => { + const app = await anApp(); + + await app.setup(); + + expect(await app.committed.mcpConfig()).not.toContain( + (await app.developmentValues()).ZITADEL_PROJECT_SECRET, + ); + }); + + it("leaves a configuration the developer wrote as it was", async () => { + const app = await anApp(); + await app.writeMcpConfig(HAND_WRITTEN_MCP_CONFIG); + + await app.setup(); + + expect(await app.committed.mcpConfig()).toBe(HAND_WRITTEN_MCP_CONFIG); + }); + }); + it.each([SCHEMA_FILE, FLOW_FILE])("writes %s", async (file) => { const app = await anApp(); const before = await app.snapshot(); diff --git a/apps/cli/tests/unit/lib/json.test.ts b/apps/cli/tests/unit/lib/json.test.ts index 0f67679936..4499840878 100644 --- a/apps/cli/tests/unit/lib/json.test.ts +++ b/apps/cli/tests/unit/lib/json.test.ts @@ -3,11 +3,45 @@ import { describe, expect, it } from "vitest"; import { deepMerge, parseJsonObject, + setJsonValue, setTopLevelJsonKey, stableStringify, wordOf, } from "@/lib/json"; +describe("setJsonValue", () => { + it("adds a member to a nested object at the document's own indent", () => { + const source = `{ + "mcpServers": { + "other": { + "type": "http" + } + } +} +`; + + const out = setJsonValue(source, ".mcp.json", ["mcpServers", "zitadel"], { type: "http" }); + + expect(out).toBe(`{ + "mcpServers": { + "other": { + "type": "http" + }, + "zitadel": { + "type": "http" + } + } +} +`); + }); + + it("creates the objects above the member when the document has none", () => { + const out = setJsonValue("{}\n", ".mcp.json", ["mcpServers", "zitadel"], { type: "http" }); + + expect(JSON.parse(out)).toEqual({ mcpServers: { zitadel: { type: "http" } } }); + }); +}); + describe("setTopLevelJsonKey", () => { it("splices one value and leaves every other byte untouched", () => { // Blank line, an inline nested object, and irregular spacing — all of it diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/guidance.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/guidance.test.ts index 58160f52bd..67aadfe88b 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/guidance.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/guidance.test.ts @@ -117,6 +117,17 @@ describe("guidance content", () => { expect(agents).toContain("Never edit `.zitadel/state.json`"); }); + it("tells agents which MCP endpoint .mcp.json connects them to", () => { + expect(agentsGuidanceSection(ctx)).toContain( + "`.mcp.json`, as the server `zitadel`: the server's endpoint http://localhost:8080/api/v1/mcp", + ); + }); + + it("names no MCP endpoint for a server that has none", () => { + const mockCtx = { ...ctx, server: "mock" } as PatchContext; + expect(agentsGuidanceSection(mockCtx)).not.toContain(".mcp.json"); + }); + it("gives the README the human journey with pinned CLI commands", () => { const readme = readmeGuidanceSection(ctx); expect(readme).toContain("from a second terminal in this directory"); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/mcp-client.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/mcp-client.test.ts new file mode 100644 index 0000000000..6dd5a4fe38 --- /dev/null +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/mcp-client.test.ts @@ -0,0 +1,120 @@ +import { exec } from "node:child_process"; +import { mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { promisify } from "node:util"; + +import { describe, expect } from "vitest"; + +import { + MCP_HEADERS_HELPER, + mcpClientOps, + mcpEndpointOf, + mcpServerEntry, + upsertMcpServer, +} from "@/lib/orca/patchers/rule/mcp-client"; +import { it } from "@tests/fixtures"; + +const execAsync = promisify(exec); + +/** The endpoint of a local server, as setup configures it. */ +const ENDPOINT = "http://localhost:8080/api/v1/mcp"; + +/** A project secret, as `.zitadel/secret` holds it. */ +const PROJECT_SECRET = "sk_proj_local"; + +/** A configuration a developer wrote for another server, at an indent of their own. */ +const ANOTHER_SERVER = `{ + "mcpServers": { + "docs": { + "type": "http", + "url": "https://docs.example.com/mcp" + } + } +} +`; + +describe("mcpEndpointOf", () => { + it.each([ + ["http://localhost:8080", ENDPOINT], + ["http://localhost:8080/", ENDPOINT], + ["https://zitadel.example.com", "https://zitadel.example.com/api/v1/mcp"], + ])("puts the endpoint of %s under the API's prefix", (server, endpoint) => { + expect(mcpEndpointOf(server)).toBe(endpoint); + }); + + it.each(["mock", "file:///tmp/server"])("finds no endpoint on %s", (server) => { + expect(mcpEndpointOf(server)).toBeUndefined(); + }); +}); + +describe("mcpClientOps", () => { + it("configures nothing for a server without an endpoint", () => { + expect(mcpClientOps("mock")).toEqual([]); + }); +}); + +describe("upsertMcpServer", () => { + describe("without a configuration", () => { + it("writes one that names the endpoint", () => { + const out = upsertMcpServer(undefined, ENDPOINT); + + expect(JSON.parse(out)).toEqual({ mcpServers: { zitadel: mcpServerEntry(ENDPOINT) } }); + }); + + it("writes the project secret as a reference, never as a value", () => { + const out = upsertMcpServer(undefined, ENDPOINT); + + expect(JSON.parse(out).mcpServers.zitadel.headers).toEqual({ + Authorization: "Bearer ${ZITADEL_PROJECT_SECRET:-}", + }); + }); + }); + + describe("with a configuration that names other servers", () => { + it("adds the endpoint beside them", () => { + const out = upsertMcpServer(ANOTHER_SERVER, ENDPOINT); + + expect(Object.keys(JSON.parse(out).mcpServers)).toEqual(["docs", "zitadel"]); + }); + + it("keeps their entries and adds its own at the developer's indent", () => { + const out = upsertMcpServer(ANOTHER_SERVER, ENDPOINT); + + expect(out).toContain( + ' "docs": {\n "type": "http",\n "url": "https://docs.example.com/mcp"\n },\n "zitadel": {\n "type": "http",', + ); + }); + }); + + it.each([ + [ + "names a zitadel server", + '{"mcpServers":{"zitadel":{"type":"http","url":"https://elsewhere"}}}', + ], + ["is not JSON", "{ // a comment\n}"], + ["is an array", "[]"], + ["holds mcpServers that are no object", '{"mcpServers":[]}'], + ])("leaves a configuration that %s as it is", (_, source) => { + expect(upsertMcpServer(source, ENDPOINT)).toBe(source); + }); + + it("changes nothing on a second run", () => { + const first = upsertMcpServer(undefined, ENDPOINT); + + expect(upsertMcpServer(first, ENDPOINT)).toBe(first); + }); +}); + +describe("MCP_HEADERS_HELPER", () => { + it("prints the project secret as the bearer a client sends", async ({ dir }) => { + await mkdir(join(dir, ".zitadel")); + await writeFile( + join(dir, ".zitadel/secret"), + JSON.stringify({ project_id: "proj_1", project_secret: PROJECT_SECRET }), + ); + + const { stdout } = await execAsync(MCP_HEADERS_HELPER, { cwd: dir }); + + expect(JSON.parse(stdout)).toEqual({ Authorization: `Bearer ${PROJECT_SECRET}` }); + }); +}); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts index 2acc9a7f8e..26bbb76ba8 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts @@ -103,13 +103,13 @@ describe("QwikRouterPatcher.plan", () => { "PUBLIC_ZITADEL_PROJECT_ID" in op.entries, ); expect(envOp?.entries).toMatchObject({ PUBLIC_ZITADEL_PROJECT_ID: "proj-1" }); - // baseOps adds AGENTS.md/README.md guidance edits to every plan; beyond + // baseOps adds the AGENTS.md/README.md guidance and .mcp.json edits to every plan; beyond // those the only config edit is the dev script's port. expect( plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md"].includes(String(op.path)), + op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual(["package.json"]); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts index a7a9cab467..4a5512e1fe 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts @@ -107,13 +107,13 @@ describe("SvelteKitPatcher.plan", () => { "VITE_ZITADEL_PROJECT_ID" in op.entries, ); expect(envOp?.entries).toMatchObject({ VITE_ZITADEL_PROJECT_ID: "proj-1" }); - // baseOps adds AGENTS.md/README.md guidance edits to every plan; beyond + // baseOps adds the AGENTS.md/README.md guidance and .mcp.json edits to every plan; beyond // those the config edits are the env declarations and the dev script's port. expect( plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md"].includes(String(op.path)), + op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual([["src/env.ts", "src/env.js"], "package.json"]); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts index 148402bb50..741f11b261 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts @@ -117,13 +117,13 @@ describe("TanStackStartPatcher.plan", () => { "VITE_ZITADEL_PROJECT_ID" in op.entries, ); expect(envOp?.entries).toMatchObject({ VITE_ZITADEL_PROJECT_ID: "proj-1" }); - // baseOps adds AGENTS.md/README.md guidance edits to every plan; beyond + // baseOps adds the AGENTS.md/README.md guidance and .mcp.json edits to every plan; beyond // those the only config edit is the dev script's port. expect( plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md"].includes(String(op.path)), + op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual(["package.json"]); diff --git a/apps/docs/content/docs/get-started/agents.mdx b/apps/docs/content/docs/get-started/agents.mdx index e20d8d10d6..022d2c1a7a 100644 --- a/apps/docs/content/docs/get-started/agents.mdx +++ b/apps/docs/content/docs/get-started/agents.mdx @@ -44,6 +44,10 @@ With the skills installed, ask in plain words. Each ask below is one the CLI can A change to sign-in is a file under `.zitadel/`, so it reaches users only when it is deployed. Ask the agent to show you `deploy --dry-run` and to commit the change before it deploys; the diff is your review. +## Connect an agent through MCP + +`setup` also writes `.mcp.json`, which connects Claude Code and the Copilot agent in VS Code to the server's MCP endpoint. There, each API operation is a tool, for an agent that works without a shell. [Connect an AI agent](/docs/guides/connect-an-agent) explains the file and how to connect other clients. + ## Invocation contract Always pass: diff --git a/apps/docs/content/docs/guides/connect-an-agent.mdx b/apps/docs/content/docs/guides/connect-an-agent.mdx new file mode 100644 index 0000000000..cd1f5d7719 --- /dev/null +++ b/apps/docs/content/docs/guides/connect-an-agent.mdx @@ -0,0 +1,69 @@ +--- +title: Connect an AI agent +description: Connect a coding agent to your project through the server's MCP endpoint, which offers each operation of the Zitadel API as a tool. +--- + +The Zitadel server answers the Model Context Protocol (MCP) at `/api/v1/mcp`. MCP is how an AI agent, such as Claude Code or the Copilot agent in VS Code, finds and calls tools. Each operation of the [Zitadel API](/docs/reference/api) is one tool there, named by its operation ID. A tool call is the operation's own request, so it is authorized, validated and recorded like the same request over HTTP. + +The endpoint takes the credential the API takes: your project secret, or a person's token. The credential's scopes decide which tools an agent is shown. A project secret covers every scope, so its agent is shown every tool. + +## Connect with the configuration setup wrote + +`zitadel setup` writes `.mcp.json` at your app's root. Claude Code and the Copilot agent in VS Code read it when you start them in that directory: + +```json +{ + "mcpServers": { + "zitadel": { + "type": "http", + "url": "http://localhost:8080/api/v1/mcp", + "headers": { + "Authorization": "Bearer ${ZITADEL_PROJECT_SECRET:-}" + }, + "headersHelper": "node -p \"JSON.stringify({Authorization:'Bearer '+JSON.parse(require('fs').readFileSync('.zitadel/secret','utf8')).project_secret})\"" + } + } +} +``` + +The file holds no secret, so commit it with the rest of your app. Each client gets the project secret its own way: + +- **Claude Code** runs `headersHelper` in your app's directory. The command reads the project secret from `.zitadel/secret`, which git ignores. Claude Code runs it only once you trust the folder, and asks before it uses a server from `.mcp.json`. +- **VS Code** runs no helper. It fills `${ZITADEL_PROJECT_SECRET:-}` from the environment VS Code was started in, and doesn't read `.env` files. Export the variable before you start VS Code: `.env.development.local` holds its value. + +Setup doesn't change a configuration you wrote. When `.mcp.json` already names a server `zitadel`, or isn't a JSON object, setup leaves the file as it is. When it names other servers, setup adds `zitadel` beside them and keeps their entries as you wrote them. + +## Connect another client + +Other clients read their own file. Add the endpoint there by hand, with the project secret from the environment: + +- **Cursor**, in `.cursor/mcp.json`: + + ```json + { + "mcpServers": { + "zitadel": { + "url": "http://localhost:8080/api/v1/mcp", + "headers": { "Authorization": "Bearer ${env:ZITADEL_PROJECT_SECRET}" } + } + } + } + ``` + +- **Codex**, in `.codex/config.toml` of a project you trust: + + ```toml + [mcp_servers.zitadel] + url = "http://localhost:8080/api/v1/mcp" + bearer_token_env_var = "ZITADEL_PROJECT_SECRET" + ``` + +Use your server's address in place of `http://localhost:8080`: it's the `server` in `zitadel.json`. + +## What the agent may do + +- **Writes ask first.** An agent is told to confirm with you before a tool that creates, changes or deletes. A call that deletes, revokes, rolls back or deploys asks you through the endpoint itself, and is refused when your client can't ask. +- **A tool's answer can hold a credential.** `publishApplication` answers with a client secret and `createInvitation` with a link that gives a role. The tool's description tells the agent to pass it to you, or store it where you keep secrets, and to write it nowhere else. +- **Its calls are marked.** A request an agent made through the endpoint carries `metadata.invoked_by: "mcp"` in its `request.api` event ([Find out what happened with events](/docs/guides/investigate-with-events)). + +An agent with a shell can do the same work with the CLI, which costs its context less: `npx @zitadel/cli@latest api ` calls any operation. diff --git a/apps/docs/content/docs/guides/meta.json b/apps/docs/content/docs/guides/meta.json index d94c638ede..1a43296538 100644 --- a/apps/docs/content/docs/guides/meta.json +++ b/apps/docs/content/docs/guides/meta.json @@ -5,6 +5,7 @@ "local-runtime", "deploy", "investigate-with-events", + "connect-an-agent", "troubleshooting", "---Sign-in---", "edit-login-flow", diff --git a/apps/docs/content/docs/sdks/angular.mdx b/apps/docs/content/docs/sdks/angular.mdx index 33df31f16e..ca64fb4ba9 100644 --- a/apps/docs/content/docs/sdks/angular.mdx +++ b/apps/docs/content/docs/sdks/angular.mdx @@ -62,7 +62,7 @@ Setup installs the SDK. To add it yourself, run: - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once and pass the handle to the standalone component. Replace `proj_123` with your project ID, which setup writes to `.env.local` as `ZITADEL_PROJECT_ID`: diff --git a/apps/docs/content/docs/sdks/next.mdx b/apps/docs/content/docs/sdks/next.mdx index 8dd6bbabd7..b008a49d67 100644 --- a/apps/docs/content/docs/sdks/next.mdx +++ b/apps/docs/content/docs/sdks/next.mdx @@ -60,7 +60,7 @@ Setup installs the SDK. To add it yourself, run: - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. When setup created the app itself, it also replaces `app/page.tsx` with a redirect to `/login`, and writes a `layout.tsx` beside the login, register and profile pages that sets their browser tab titles. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). When setup created the app itself, it also replaces `app/page.tsx` with a redirect to `/login`, and writes a `layout.tsx` beside the login, register and profile pages that sets their browser tab titles. Create `proxy.ts` next to your `app` directory (at the project root, or in `src/` when you use `src/app`) and call the middleware. On Next 15, name the file `middleware.ts` and the function `middleware`. diff --git a/apps/docs/content/docs/sdks/nuxt.mdx b/apps/docs/content/docs/sdks/nuxt.mdx index 7b618a02ad..adb99d6b4c 100644 --- a/apps/docs/content/docs/sdks/nuxt.mdx +++ b/apps/docs/content/docs/sdks/nuxt.mdx @@ -59,7 +59,7 @@ Setup installs the SDK. To add it yourself, run: - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. When setup created the app itself, it also writes `app/app.vue` and an `app/pages/index.vue` that redirects to `/login`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). When setup created the app itself, it also writes `app/app.vue` and an `app/pages/index.vue` that redirects to `/login`. Register the SDK's Nuxt module and configure it under `zitadel`. The module adds the server middleware, the plugin that exposes auth state, and the client plugin that loads the web components. diff --git a/apps/docs/content/docs/sdks/qwik-router.mdx b/apps/docs/content/docs/sdks/qwik-router.mdx index bec3cd8b8a..cba3097920 100644 --- a/apps/docs/content/docs/sdks/qwik-router.mdx +++ b/apps/docs/content/docs/sdks/qwik-router.mdx @@ -60,7 +60,7 @@ Install `@zitadel/sdk-qwik-router`. It re-exports the Qwik widgets, so the app n - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Create `src/routes/plugin@zitadel.ts`. The handler reads `ZITADEL_URL`, `ZITADEL_PROJECT_SECRET`, and `ZITADEL_PROJECT_ID` per request from the request event's `env`, which is where Vite loads `.env.local`: diff --git a/apps/docs/content/docs/sdks/qwik.mdx b/apps/docs/content/docs/sdks/qwik.mdx index d2bb7293f3..86f0a4fe50 100644 --- a/apps/docs/content/docs/sdks/qwik.mdx +++ b/apps/docs/content/docs/sdks/qwik.mdx @@ -61,7 +61,7 @@ Setup installs the SDK. To add it yourself, run: - In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once, then render the login and logout components. `VITE_ZITADEL_PROJECT_ID` is the project ID that setup wrote to `.env.local`: diff --git a/apps/docs/content/docs/sdks/react.mdx b/apps/docs/content/docs/sdks/react.mdx index 24761956fa..20ff560956 100644 --- a/apps/docs/content/docs/sdks/react.mdx +++ b/apps/docs/content/docs/sdks/react.mdx @@ -57,7 +57,7 @@ Setup installs the SDK. To add it yourself, run: - In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once, then render the login and logout components. `VITE_ZITADEL_PROJECT_ID` is the project ID that setup wrote to `.env.local`: diff --git a/apps/docs/content/docs/sdks/solid-start.mdx b/apps/docs/content/docs/sdks/solid-start.mdx index 532102784b..909b83688e 100644 --- a/apps/docs/content/docs/sdks/solid-start.mdx +++ b/apps/docs/content/docs/sdks/solid-start.mdx @@ -61,7 +61,7 @@ Install `@zitadel/sdk-solid-start`. It re-exports the Solid widgets, so the app - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Create `src/middleware.ts`. SolidStart 2 registers this module's default export as h3 middleware, so wrap the config in its `createMiddleware`; a bare config object registers nothing. diff --git a/apps/docs/content/docs/sdks/solid.mdx b/apps/docs/content/docs/sdks/solid.mdx index a7c74f32e5..2f7e80f102 100644 --- a/apps/docs/content/docs/sdks/solid.mdx +++ b/apps/docs/content/docs/sdks/solid.mdx @@ -57,7 +57,7 @@ Setup installs the SDK. To add it yourself, run: - In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once, then render the login and logout components. `VITE_ZITADEL_PROJECT_ID` is the project ID that setup wrote to `.env.local`: diff --git a/apps/docs/content/docs/sdks/svelte.mdx b/apps/docs/content/docs/sdks/svelte.mdx index 6c7b684b9f..e6491490a8 100644 --- a/apps/docs/content/docs/sdks/svelte.mdx +++ b/apps/docs/content/docs/sdks/svelte.mdx @@ -57,7 +57,7 @@ Setup installs the SDK. To add it yourself, run: - In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once, then render the login and logout components. `VITE_ZITADEL_PROJECT_ID` is the project ID that setup wrote to `.env.local`: diff --git a/apps/docs/content/docs/sdks/sveltekit.mdx b/apps/docs/content/docs/sdks/sveltekit.mdx index 1617bc510f..656b7575d6 100644 --- a/apps/docs/content/docs/sdks/sveltekit.mdx +++ b/apps/docs/content/docs/sdks/sveltekit.mdx @@ -62,7 +62,7 @@ Install `@zitadel/sdk-sveltekit`. It re-exports the Svelte widgets, so the app n - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Declare the hook's settings in `src/env.ts`, so SvelteKit serves them from the server-only `$app/env/private`: from `.env.local` in development and from the platform's environment in production. A missing one fails each request with an error naming it, and SvelteKit never bundles them for the browser. If the app already has `src/env.ts`, add the three entries to its `defineEnvVars` call: diff --git a/apps/docs/content/docs/sdks/tanstack-start.mdx b/apps/docs/content/docs/sdks/tanstack-start.mdx index 42611affdf..dc72c1af4c 100644 --- a/apps/docs/content/docs/sdks/tanstack-start.mdx +++ b/apps/docs/content/docs/sdks/tanstack-start.mdx @@ -60,7 +60,7 @@ Setup installs the SDK. To add it yourself, run: - Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Create `src/start.ts`. Having this file opts the app out of TanStack Start's automatic CSRF protection for server functions, so restore it and run it before auth: diff --git a/apps/docs/content/docs/sdks/vue.mdx b/apps/docs/content/docs/sdks/vue.mdx index 13d3ff79ae..0223ac5bf2 100644 --- a/apps/docs/content/docs/sdks/vue.mdx +++ b/apps/docs/content/docs/sdks/vue.mdx @@ -57,7 +57,7 @@ Setup installs the SDK. To add it yourself, run: - In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`. + In an existing app, the CLI edits the `vite.config.*` you already have. Every setup also writes `zitadel.json` and the `.zitadel/` folder, adds its entries to `.gitignore`, and adds managed sections to `AGENTS.md` and `README.md`, and configures an AI agent in `.mcp.json` ([Connect an AI agent](/docs/guides/connect-an-agent)). Configure Zitadel once, then render the components. `VITE_ZITADEL_PROJECT_ID` is the project ID that setup wrote to `.env.local`: diff --git a/docs/adrs/092-mcp-server.md b/docs/adrs/092-mcp-server.md index f8fe3b579d..d96bddc824 100644 --- a/docs/adrs/092-mcp-server.md +++ b/docs/adrs/092-mcp-server.md @@ -660,7 +660,17 @@ talks to, without a separate install. It is wanted and not built. ## Open questions -1. Does `setup` write the client's configuration? +1. Decided on 2026-12-02: `setup` writes the client's configuration, a root + `.mcp.json`, which Claude Code and the Copilot agent in VS Code read. It + names the endpoint as the server `zitadel` and holds no secret, since the + file is committed: its `headersHelper`, which Claude Code runs in the + project's directory once the person trusts the folder, reads the project + secret from the git-ignored `.zitadel/secret`, and its `headers` name + `${ZITADEL_PROJECT_SECRET}` for a client that fills it from its + environment. A file that already names `zitadel`, or that is not a JSON + object, is left as it is. Open: Cursor and Codex read files of their own + (`.cursor/mcp.json`, `.codex/config.toml`); the guide shows them, and + setup writes neither yet. 2. Does a `PATCH` whose `null` removes ask, on `zitadel api` and here? 3. A lint that makes every operation a project secret authenticates declare its plane, and that fails on a response field holding a credential whose @@ -777,4 +787,5 @@ talks to, without a separate install. It is wanted and not built. [`nextgen.example.yaml`](../operations/nextgen.example.yaml): `mcp.enabled`. - [`api/openapi/AGENTS.md`](../../apps/api/api/openapi/AGENTS.md): the three markers. -- Not yet written: a guide in the docs site on connecting an MCP client. +- [Connect an AI agent](../../apps/docs/content/docs/guides/connect-an-agent.mdx): + connecting an MCP client, and the `.mcp.json` `setup` writes. From b33dbd0b7963690eac6a398fbfa974b3d99fef7b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 08:04:52 +0000 Subject: [PATCH 2/2] fix: keep setup's .mcp.json out of the conformance fixtures The fixture generator drops what setup writes that the app never reads. .mcp.json is an agent's configuration, and names the server setup ran against rather than the one the lane serves, so it is dropped as zitadel.json, AGENTS.md and README.md are, and the fixtures do not drift with the port they were generated on. Also formats the three patcher tests the .mcp.json edit touched. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0137eGmSarR3gn9oz3Btwd8H --- .../unit/lib/orca/patchers/rule/qwik-router/index.test.ts | 3 ++- .../unit/lib/orca/patchers/rule/sveltekit/index.test.ts | 3 ++- .../unit/lib/orca/patchers/rule/tanstack-start/index.test.ts | 3 ++- conformance/suite/bin/conformance-fixture.mjs | 5 ++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts index 26bbb76ba8..b53282df46 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/qwik-router/index.test.ts @@ -109,7 +109,8 @@ describe("QwikRouterPatcher.plan", () => { plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), + op.kind === "edit" && + !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual(["package.json"]); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts index 4a5512e1fe..278ded85bc 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/sveltekit/index.test.ts @@ -113,7 +113,8 @@ describe("SvelteKitPatcher.plan", () => { plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), + op.kind === "edit" && + !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual([["src/env.ts", "src/env.js"], "package.json"]); diff --git a/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts b/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts index 741f11b261..c54de87fe3 100644 --- a/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts +++ b/apps/cli/tests/unit/lib/orca/patchers/rule/tanstack-start/index.test.ts @@ -123,7 +123,8 @@ describe("TanStackStartPatcher.plan", () => { plan.ops .filter( (op): op is Extract => - op.kind === "edit" && !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), + op.kind === "edit" && + !["AGENTS.md", "README.md", ".mcp.json"].includes(String(op.path)), ) .map((op) => op.path), ).toEqual(["package.json"]); diff --git a/conformance/suite/bin/conformance-fixture.mjs b/conformance/suite/bin/conformance-fixture.mjs index 63c63d24dd..885241b697 100755 --- a/conformance/suite/bin/conformance-fixture.mjs +++ b/conformance/suite/bin/conformance-fixture.mjs @@ -28,7 +28,9 @@ const HARNESS_FILES = new Set(["playwright.config.mts", "conformance.spec.ts", " * framework, beyond what the app's own `.gitignore` leaves out (`appFiles`): * install output; the secret and the env files that hold it, whether or not * the app ignores them; the CLI's own state, which the app never reads - * (`zitadel.json`, `.zitadel/`); the docs setup writes for a person; and + * (`zitadel.json`, `.zitadel/`); the docs setup writes for a person; the + * MCP client's configuration it writes for an agent (`.mcp.json`), which + * names the server setup ran against and not the one the lane serves; and * editor and package-manager settings, which pnpm and the workspace own. */ const DROPPED_DIRS = ["node_modules", ".zitadel", ".vscode"]; @@ -39,6 +41,7 @@ const DROPPED_FILES = new Set([ "zitadel.json", "AGENTS.md", "README.md", + ".mcp.json", ]); /**