{"slug": "building-a-hosted-mcp-server-for-architecture-docs", "title": "Building a hosted MCP server for architecture docs", "summary": "The ArchDocs team built a hosted Model Context Protocol server for architecture documentation that binds each request to a user and organization identity, using OAuth authorization codes with PKCE and a fresh McpServer and StreamableHTTPServerTransport per request with sessionIdGenerator set to undefined. The server's update_doc tool delegates to DslService.compileExisting, which finds the document in the organization, compiles the supplied YAML and calls ArchitectureDocsService.update to store the YAML and refreshed layout and emit a recompile event, while get_doc returns stored YAML for code-backed documents. The team notes that a token identifying an organization does not answer whether an individual document operation should be allowed, so that check stays in the domain services, and that exchanging an authorization code consumes it before the remaining checks run, meaning a failed PKCE verifier requires restarting authorization.", "body_md": "# Building a hosted MCP server for architecture docs\n\nA request-bound MCP server, OAuth with PKCE, and YAML compilation through the existing document services, including the limits of validation.\n\n- Author\n- The ArchDocs team\n- Published\n- Reading\n- 4 min read\n\nAn agent asked to add a service needs to read the current document before it writes a replacement. On a hosted server, that read also needs a user and an organization; a document ID by itself is insufficient context.\n\nWe built the MCP endpoint around those constraints. The protocol handler gets a request-bound identity, and the tools call the existing document services to read or compile YAML.\n\n## [Bind the server to the request](#bind-the-server-to-the-request)\n\nThe authentication guard reads the bearer token and asks the verifier to check it. The verifier accepts either an ArchDocs personal access token or an OAuth access token and returns the user and organization IDs. The guard loads those records and attaches them to the request before the controller creates the MCP server.\n\nThe factory closes over that principal. Tool arguments carry document IDs and source text, rather than a user-supplied organization ID. This removes an opportunity for a tool call to select a different tenant, though the services still need to enforce access on each operation.\n\nOur controller creates a fresh `McpServer` and `StreamableHTTPServerTransport` for each request. The transport has `sessionIdGenerator: undefined`. When the response closes, the controller closes both objects.\n\n``` js\nconst server = factory.create(req.mcpUser, req.mcpOrg)\nconst transport = new StreamableHTTPServerTransport({\n  sessionIdGenerator: undefined,\n})\nres.on('close', () => {\n  transport.close()\n  server.close()\n})\nawait server.connect(transport)\nawait transport.handleRequest(req, res, req.body)\n```\n\nThis excerpt leaves out the Nest decorators and guard wiring. The trade-off is that a tool can't rely on conversation state held inside a previous server instance. State that needs to survive a request belongs in storage. OAuth clients and tokens are stored there too; stateless transport doesn't make authentication stateless.\n\n## [Let clients discover authorization](#let-clients-discover-authorization)\n\nFor interactive clients, our OAuth path uses authorization codes with PKCE. Protected-resource metadata points to authorization-server metadata, which advertises the registration, authorization and token endpoints. A missing or invalid bearer token gets a `WWW-Authenticate` challenge with the protected-resource metadata URL.\n\nDynamic registration stores the client's redirect URIs. Authorization checks the requested redirect against that list. The code records the selected organization and the PKCE challenge. Exchange requires a verifier and compares its hash with the stored challenge.\n\nAn implementation detail worth testing: exchanging a code consumes it before the remaining code checks run. A failed verifier means starting authorization again. Refresh revokes the old token record and creates a new one, and requested refresh scopes are intersected with the scopes already granted.\n\nA token identifying an organization doesn't answer whether an individual document operation should be allowed. Keep that question in the domain services and test it there.\n\n## [Compile through the document services](#compile-through-the-document-services)\n\n`update_doc` delegates to `DslService.compileExisting`. That service finds the document in the organization, loads its saved layout and compiles the supplied YAML. On successful compilation, it calls `ArchitectureDocsService.update`, stores the YAML and refreshed layout, and emits a recompile event.\n\nSaving element JSON directly would skip document behavior owned by the service. MCP shouldn't grow another implementation of reference bookkeeping just because its caller is an agent.\n\n`get_doc` returns stored YAML for a code-backed document. For a canvas document without stored source, it decompiles the elements. `patch_doc` reads that YAML, applies operations addressed by node ID or relationship endpoints, then sends the patched YAML through the same compile path as a replacement.\n\nPatching makes a small edit easier to express. It still recompiles the document. It also doesn't provide compare-and-swap: the tool input has no expected revision field. If a person and an agent edit concurrently, read again before writing and inspect the result. A successful patch is no guarantee that another writer's recent change was preserved.\n\n## [Validation has a narrower job than review](#validation-has-a-narrower-job-than-review)\n\n`validate_yaml` performs a dry-run compile without creating a document. It catches schema errors and unresolved relationship endpoints before a write. `get_schema` gives the agent the current construct reference, so the model doesn't have to guess the fields of an endpoint card.\n\nThere is a reason to validate before calling `create_doc`: the creation service makes a blank document before compiling into it. An invalid source can fail after that blank document has been created. The tool should not be described as an atomic validate-and-create transaction.\n\nEven a clean dry run says nothing about whether a proposed dependency exists in the deployed system. Review the YAML and the diagram as a design change. The same distinction applies to [architecture as code](https://archdocs.io/blog/topics/arch-as-code) without an agent.\n\nArchDocs exposes this path so an agent can edit the same document a person opens on the [structured canvas](https://archdocs.io/blog/forking-excalidraw-for-structured-diagrams). The [MCP overview](https://archdocs.io/docs/mcp/overview) starts with the OAuth setup for clients. For a first edit, ask the agent to read an existing document, change a named node, validate the source and show the changed YAML before submitting it.\n\n**The ArchDocs team** 4 min read\n\nMore in Architecture as code\n\nStart", "url": "https://wpnews.pro/news/building-a-hosted-mcp-server-for-architecture-docs", "canonical_source": "https://archdocs.io/blog/building-a-hosted-mcp-server", "published_at": "2026-10-23 00:00:00+00:00", "updated_at": "2026-11-02 18:20:51.274035+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "developer-tools", "ai-tools"], "entities": ["ArchDocs", "Model Context Protocol", "McpServer", "StreamableHTTPServerTransport", "DslService", "ArchitectureDocsService"], "also_reported_by": [], "takeaway": "Binding the MCP server to a request-scoped principal and passing document IDs rather than user-supplied organization IDs removes cross-tenant selection, but per-operation access still must be enforced in the domain services, and because exchanging an OAuth code consumes it before the PKCE verifier c", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/building-a-hosted-mcp-server-for-architecture-docs", "markdown": "https://wpnews.pro/news/building-a-hosted-mcp-server-for-architecture-docs.md", "text": "https://wpnews.pro/news/building-a-hosted-mcp-server-for-architecture-docs.txt", "jsonld": "https://wpnews.pro/news/building-a-hosted-mcp-server-for-architecture-docs.jsonld"}}