MCP server, authentication, and tools
The MCP surface lets external clients use nao for project-scoped analytics, stories, charts, and administration. It also lets a project connect to outside MCP servers and make their tools available to nao agents. Authentication, project settings, tool policy, and result rendering form one controlled path from client discovery to tool output. See MCP and external API for the wider API context and Authentication and SSO for related sign-in flows.
MCP endpoint
The MCP endpoint is the inbound interface for clients such as desktop assistants and other MCP hosts. It exposes a stateless HTTP connection to a server built for the authenticated user and selected project.
Endpoint discovery
The endpoint accepts POST requests at /mcp and /mcp/:projectId. A bare /mcp URL works when the user has one project. A user with several projects must use the project-specific URL so the request has one clear target. The endpoint uses streamable HTTP and rejects other methods in stateless mode. apps/…/routes.ts• L24-45 apps/…/project.ts• L5-29
MCP clients discover authentication through protected-resource metadata. The service publishes metadata at the root well-known URL and at paths tied to the bare and project-scoped MCP URLs. An unauthenticated MCP response also points the client to this metadata through WWW-Authenticate, so a client can start discovery from a normal failed request. apps/…/app.ts• L351-369 apps/…/routes.ts• L120-127
References: apps/…/routes.ts• L24-45 apps/…/project.ts• L5-29 apps/…/app.ts• L351-369
Request authentication
MCP requests may use the signed nao session or a bearer access token. Bearer verification accepts a JWT issued for an allowed MCP resource, checks its issuer and audience, and reads the user from sub. It uses nao's local signing keys, which avoids a network call back to the issuer. If the token is opaque instead of a JWT, the service checks its hashed value and expiry in the token store. Deactivated users and viewers cannot use the endpoint. apps/…/auth.ts• L13-25 apps/…/auth.ts• L27-71 apps/…/auth.ts• L99-112
The authentication result supplies the user identity to project resolution and tool creation. This keeps a valid token from granting access to a project or role that the user does not have. apps/…/routes.ts• L47-70
References: apps/…/auth.ts• L13-71 apps/…/auth.ts• L99-128 apps/…/routes.ts• L47-70
Public metadata and issuer URLs
The normal MCP resource URL is derived from BETTER_AUTH_URL. A deployment can set MCP_PUBLIC_URL when external clients must reach a public MCP host while the user interface stays on a private host. Tokens then accept the internal and public MCP resource audiences. apps/…/env.ts• L382-393 apps/…/env.ts• L639-654
Metadata and the WWW-Authenticate value describe the host that the client used. On the public host, server-to-server authorization URLs, the issuer, and JWKS URLs are rebased to that host. Browser login and consent URLs remain on the UI host. The authorization flow also rewrites the issuer in redirects and newly issued JWTs for public-resource flows, while verification accepts both the normal and public issuer. This keeps discovery, consent, and token checks consistent across split-host deployments. apps/…/app.ts• L351-363 apps/…/public-auth-metadata.ts• L29-57 apps/…/public-issuer.ts• L23-39 apps/…/auth.ts• L99-112
References: apps/…/env.ts• L382-393 apps/…/env.ts• L639-674 apps/…/public-auth-metadata.ts• L29-57 apps/…/public-issuer.ts• L23-39 apps/…/auth.ts• L99-128
Project configuration
MCP behavior is scoped to a project. The inbound endpoint reads project settings and the outbound server manager reads the project's MCP configuration, so the same user can see different tools and policies in different projects.
Endpoint settings
Each project controls whether its inbound MCP endpoint is enabled. It also controls the sub-agent mode, context-layer mode, and optional model used by ask_nao. Stored project settings override the deployment default. The endpoint checks the enabled flag before it creates the MCP server. apps/…/mcp-endpoint.ts• L6-18 apps/…/mcp-endpoint.queries.ts• L9-37 apps/…/routes.ts• L73-87
The server builds its tool set from these settings and from the caller's project role and feature access. This means a tool can be absent because its mode is off, its feature is not available, or the caller lacks the required role. apps/…/server.ts• L17-57
References: apps/…/mcp-endpoint.ts• L6-18 apps/…/mcp-endpoint.queries.ts• L9-37 apps/…/routes.ts• L73-87 apps/…/server.ts• L17-57
External server configuration
The project file agent/…/mcp.json declares external MCP servers. A declaration can describe an HTTP server or a local command server, provide headers or environment values, and name a pre-registered OAuth client. Project environment values are resolved before the configuration is validated. Cloud deployments allow only HTTP transport. apps/…/mcp.ts• L12-23 apps/…/mcp.ts• L63-86 apps/…/mcp.ts• L632-689 apps/…/mcp.ts• L1028-1041
The service watches this project configuration and refreshes server discovery after a change. Invalid configuration is reported as a project MCP error instead of being used to create a runtime. apps/…/mcp.ts• L199-241 apps/…/mcp.ts• L710-733 apps/…/mcp.ts• L1183-1200
The service discovers external tools without placing every tool in the agent's context. Project admins can disable a server or an individual tool. User-group grants provide a second access check, so a tool must be both enabled by the project and granted to the caller. apps/…/mcp.ts• L217-245 apps/…/mcp.ts• L432-475
References: apps/…/mcp.ts• L12-23 apps/…/mcp.ts• L63-86 apps/…/mcp.ts• L199-241 apps/…/mcp.ts• L632-711 apps/…/mcp.ts• L217-245
External OAuth connections
An external HTTP server is treated as OAuth-protected when it advertises protected-resource metadata, unless the project supplies a static credential. A declared OAuth client takes precedence over dynamic client registration. The user completes an authorization-code flow with PKCE, and the resulting access and refresh tokens remain scoped to the user, project, and server. Expired tokens refresh when possible; an invalid token requires the user to connect again. apps/…/mcp-oauth.ts• L49-57 apps/…/mcp-oauth.ts• L70-117 apps/…/mcp-oauth.ts• L165-217
Tool discovery needs one usable token. An admin normally owns the project's discovery connection. If no admin has connected, the first eligible user can provide discovery. Later calls still use the calling user's token, which prevents one user's external account from authorizing another user's tool call. apps/…/mcp-oauth.ts• L112-123 apps/…/mcp.ts• L365-429 apps/…/mcp.ts• L754-799
References: apps/…/mcp-oauth.ts• L49-57 apps/…/mcp-oauth.ts• L70-117 apps/…/mcp-oauth.ts• L125-273 apps/…/mcp.ts• L365-429
Tool surface
The MCP server presents a small, policy-aware tool surface to inbound clients. External server tools follow a separate discovery path and become available to nao agents on demand.
Built-in tool registration
Built-in tools are registered when a project MCP server is created. The registration layer gives each tool a name, description, input schema, output schema, and optional UI metadata. A shared wrapper checks the tool's enabled MCP modes, records the call, and reports success or failure. Agent tools can use the same layer, which keeps direct MCP tools and existing nao capabilities on one contract. apps/…/register-mcp-tool.ts• L9-48 apps/…/register-mcp-tool.ts• L58-87 apps/…/register-mcp-tool.ts• L105-127 apps/…/logging.ts• L35-91
The main groups are:
| Group | Product purpose |
|---|---|
| Sub-agent tools | Ask nao analytics questions, continue long runs, and create or update stories. |
| Context and data tools | Read project context, run SQL or semantic queries, and use query results. |
| Asset tools | Render charts and maps and list, read, archive, or delete stories. |
| Context administration | Search and inspect project chat history, run project tests, and view recommendations for permitted roles. |
| Member budgets | Export member usage and budget data for licensed admins. |
| Personal skill tools | Let eligible users list and create their own instructions for recurring nao requests. |
| MCP apps | Return interactive story, chart, and map resources to compatible clients. |
Context administration is reserved for admin and context_admin roles. Its chat tools support project-wide search and transcript review, while its test tools expose test discovery, production-versus-change runs, result summaries, and background-run control. Chat output limits large tool inputs and outputs unless the caller requests more detail. apps/…/context-admin-tools.ts• L9-26 apps/…/chat-history.ts• L19-32 apps/…/chat-history.ts• L97-222 apps/…/tests.ts• L20-45 apps/…/tests.ts• L67-73
These groups are conditional. Modes, project features, user roles, licensing, and user-group access decide which tools appear and which calls succeed. The call wrapper checks mode settings again at run time, so a disabled mode returns an MCP error even when a client still has an older tool listing. apps/…/server.ts• L31-55 apps/…/context-admin-tools.ts• L9-24 apps/…/member-budget.ts• L30-57 apps/…/logging.ts• L53-61
The context and data group also follows the project's data mode. Semantic queries appear only when the semantic layer is enabled. SQL is limited to local data when warehouse SQL is disabled, and the input contract reflects the databases that the project can use. Query results receive reusable IDs and structured data so later chart and story tools can use the same data. Semantic-query output hides compiled SQL while keeping the result contract aligned with SQL results. apps/…/context-layer.ts• L120-245
The context and story tools expose project-file browsing, governed metric queries, SQL, and story creation or updates. SQL and semantic-query results are stored with reusable query IDs. Story tools can use those results for embedded charts and tables. SQL input also changes with project data access: warehouse selection is required when several warehouse databases are available, while local-only projects expose only the local database. apps/…/context-layer.ts• L125-215 apps/…/context-layer.ts• L223-292
Story tools support both classic markdown stories and custom interactive stories. A custom story uses app files instead of markdown content. The service keeps drafts and published versions separate, checks project and chat scope, and blocks overlapping MCP edits with a story lock. apps/…/context-layer.ts• L287-437 apps/…/custom-story-mcp.ts• L70-190 apps/…/custom-story-mcp.ts• L217-294
Agent-backed tools run either in the caller's project context or in a chat that belongs to that caller and project. A requested chat that fails this check is not used, so MCP calls cannot borrow another user's conversation context. Long ask_nao work can continue after the request ends; its run state reports running, complete, or error for get_nao_answer polling. apps/…/run-agent-tool.ts• L8-25 apps/…/ask-nao-runs.ts• L37-81 apps/…/sub-agent.ts• L546-582 apps/…/sub-agent.ts• L385-390
References: apps/…/register-mcp-tool.ts• L9-48 apps/…/logging.ts• L35-110 apps/…/server.ts• L31-57 apps/…/asset-tools.ts• L85-89 apps/…/context-layer.ts• L120-292 apps/…/custom-story-mcp.ts• L70-190 apps/…/run-agent-tool.ts• L8-25 apps/…/ask-nao-runs.ts• L37-96 apps/…/sub-agent.ts• L129-244 apps/…/context-admin-tools.ts• L9-26 apps/…/chat-history.ts• L97-222 apps/…/tests.ts• L67-266 apps/…/member-budget.ts• L30-57 apps/…/personal-skills.ts• L29-95 apps/…/user-skill.service.ts• L17-49 apps/…/user-group-features.ts• L167-184
Personal skill tools
Personal skills are user-owned instructions that nao can use for recurring requests. MCP exposes list and create actions for eligible users, while the normal nao agent can use those skills in later chats. apps/…/personal-skills.ts• L12-27 apps/…/user-skill.service.ts• L18-35
Access follows the same project and role policy as the application. Admins and context admins can manage personal skills. A user role needs a user-group grant for the personalSkills feature. Other roles do not receive these tools. apps/…/personal-skills.ts• L29-40 apps/…/user-group-features.ts• L167-184
Personal skill names cannot conflict with another personal skill owned by the user or with a project skill. The create action returns the skill name, usage description, and sharing status. It does not publish the instructions as a project skill. apps/…/personal-skills.ts• L43-95 apps/…/user-skill.service.ts• L37-49
References: apps/…/personal-skills.ts• L29-95 apps/…/user-skill.service.ts• L17-49 apps/…/user-group-features.ts• L167-184 apps/…/load-skill.ts• L22-80
External tool discovery
For each configured external server, nao lists the available tools and writes one generated OpenAPI 3.1 description per enabled tool under the project MCP directory. The agent can inspect a description when needed and calls the remote operation through mcp_call. This keeps large tool catalogs out of the normal agent context while preserving each tool's name, description, and input shape. apps/…/mcp.ts• L126-133 apps/…/mcp.ts• L720-778 apps/…/mcp-openapi.ts• L31-80
Before dispatch, nao checks project and group access and validates arguments against the discovered input schema when it can compile that schema. If discovery has no usable schema, the remote server remains the final authority. apps/…/mcp.ts• L432-475 apps/…/mcp.ts• L535-569
References: apps/…/mcp.ts• L126-133 apps/…/mcp.ts• L720-778 apps/…/mcp-openapi.ts• L31-80 apps/…/mcp.ts• L432-475
Schema dialect support
Inbound tool listings remove an explicit $schema dialect from input and output schemas. The MCP SDK can add a draft-07 marker to schemas made from Zod, while some clients only accept JSON Schema 2020-12. Omitting the marker lets clients use their supported default without changing the schema's fields. apps/…/tool-schema-dialect.ts• L11-23 apps/…/tool-schema-dialect.ts• L25-47
Outbound tool descriptions use OpenAPI 3.1 and preserve a sanitized JSON Schema for each tool's request body. The same discovered schema is used for local argument checks, so the description shown to the agent and the validation applied before a call stay aligned. apps/…/mcp-openapi.ts• L43-60 apps/…/mcp.ts• L571-613
References: apps/…/tool-schema-dialect.ts• L11-47 apps/…/mcp-openapi.ts• L31-80 apps/…/mcp.ts• L535-613
Embedded results
MCP tool results can carry both machine-readable data and a client-rendered view. nao registers MCP Apps resources for stories, charts, and maps, then returns an embed URL or a self-contained HTML fallback with the structured result.
MCP app resources
The server advertises three UI resources: a story app, a chart app, and a map app. Each resource provides an app shell that loads the MCP Apps browser bundle and receives tool results from the host. The bundle is also served from /mcp/mcp-apps.js for compatible clients. apps/…/ui-resources.ts• L9-64 apps/…/routes.ts• L27-33 apps/…/mcp-apps-bundle.ts• L5-15
Chart and map results can point to a stored embed and include a query or chat link. The server checks that the result data matches the requested chart or map before it creates that link. It can also return a self-contained view for clients that cannot load the embed URL. apps/…/helpers.ts• L91-130 apps/…/helpers.ts• L132-251 apps/…/helpers.ts• L256-335
Story results can point to a rendered story. Classic stories can use the MCP app frame or a self-contained view. Custom stories are interactive apps that are built and published as versions, so their MCP result is a nao link rather than an MCP app embed. A custom story can remain an unpublished draft with source files, and build errors leave the prior published version in place. Chat-attached custom stories use query data from that chat, while standalone stories store the rows needed by their source files. apps/…/custom-story-mcp.ts• L30-43 apps/…/custom-story-mcp.ts• L85-156 apps/…/custom-story-mcp.ts• L245-294 apps/…/embed-tool-result.ts• L46-64 apps/…/embed-tool-result.ts• L91-112 apps/…/embed-tool-result.ts• L161-186
References: apps/…/ui-resources.ts• L9-64 apps/…/routes.ts• L27-33 apps/…/mcp-apps-bundle.ts• L5-39 apps/…/helpers.ts• L91-335 apps/…/custom-story-mcp.ts• L30-43 apps/…/custom-story-mcp.ts• L85-294 apps/…/embed-tool-result.ts• L46-112
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the MCP endpoint paths, protected-resource metadata paths,
WWW-Authenticateresource URL, and public-host URL rules aligned. - Keep JWT issuer and audience validation aligned with the resource URLs advertised to clients.
- Keep project endpoint settings aligned with the conditional tool registration branches and the UI settings contract.
- Keep external-server config validation aligned with OAuth detection, static credentials, and the cloud transport rule.
- Keep generated OpenAPI tool descriptions aligned with argument validation and the agent's
mcp_callcontract. - Keep MCP Apps resource URIs, tool
_metavalues, embed payload fields, and the browser bundle URL aligned. - Keep data mode settings aligned with the SQL and semantic-query schemas exposed by context-layer.ts.
- Keep SQL database selection rules aligned with the configured database list and the local-only fallback schema.
- Keep classic and custom story input rules aligned with the result shape and publish behavior.
- Keep chat and project ownership checks aligned before reusing query data or changing a story.
- Keep agent tool execution aligned with the rule that a requested chat must belong to the caller and project.
- Keep
ask_naostatus payloads, background run retention, and polling behavior aligned across the initial call andget_nao_answer. - Keep MCP call logging, mode gates, and tool names aligned so disabled tools fail clearly and usage data remains attributable.
- Keep custom story edit locks aligned with all MCP write paths so concurrent calls do not overwrite a draft.
- Keep context-admin role gates aligned across chat history, recommendation, and test tools.
- Keep test-run start, status, comparison, and cancel payloads aligned with background polling clients.
- Keep chat search filters, transcript ownership checks, and output truncation aligned with the context-admin tool contract.
- Keep external OAuth discovery credentials separate from the caller's per-request access token.
- Keep config reload and generated tool specs aligned so removed or disabled tools do not remain callable.
- Keep personal-skill MCP registration aligned with the role and user-group feature gate, name conflict rules, and agent skill visibility.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A client uses bare /mcp for a user with several projects | The request cannot select a project | Use /mcp/:projectId from the discovery error. |
| A token has the wrong issuer or MCP resource audience | The endpoint returns unauthorized | Mint the token for the advertised MCP resource and issuer. |
| A split deployment advertises the private UI host to a public client | Discovery or token calls cannot reach the auth host | Set MCP_PUBLIC_URL and use the public MCP URL. |
| A project disables the endpoint or a server/tool | The MCP server or call is unavailable | Check project settings and effective group access. |
| An OAuth access or refresh token expires or is revoked | Discovery or a tool call requires reconnection | Complete the server connection flow again. |
| An external tool schema cannot be compiled | Local argument validation is skipped | Let the remote MCP server validate the request. |
| A client rejects the SDK's draft-07 schema marker | Tool listing fails in that client | Use the dialect-stripping transport for inbound listings. |
| A host blocks an embed URL | The interactive result does not render | Use the structured result's inline HTML fallback or the nao link. |
| A custom story build has errors | The new version is not published | Read the returned build errors and update the source files before publishing again. |
| A chart or map refers to missing query columns | The result cannot produce the requested visual | Use the available columns from the query result when building the artifact. |
| A client sends a chat ID outside the caller's project | The agent tool runs without that chat context or rejects the operation | Use a chat ID owned by the caller in the selected project. |
ask_nao exceeds its synchronous wait budget | The client receives running instead of the final answer | Poll get_nao_answer with the returned chat ID. |
| Two MCP calls edit one custom story at the same time | One call is refused instead of corrupting the draft | Wait for the active edit to finish before retrying. |
| A project mode is disabled after tool discovery | The stale tool listing returns a mode-disabled MCP error | Refresh project settings or use a tool in an enabled mode. |
| An SQL request names an unavailable database or omits a required database in a multi-warehouse project | The request is rejected before query execution | Use a configured database ID and select one when multiple warehouses are enabled. |
| A non-admin calls context-admin tools | Chat, recommendation, or test operations are not registered for the caller | Use an admin or context_admin account. |
| A test run is still queued or running | Results are incomplete or a second run cannot start for the project | Poll get_test_run and use cancel_test_run when needed. |
| mcp.json contains invalid JSON, unresolved project variables, or a cloud stdio server | The server is not discovered or cannot be used | Fix the project config and use an allowed HTTP transport in cloud mode. |
| A stale generated spec remains after a server or tool is disabled | The agent can see a tool that policy no longer allows | Reapply enablement or refresh discovery before calling the tool. |
| A user without personal-skill access calls the personal skill tools | The tools are not registered for that caller | Use an admin, context admin, or a user in a group with the personalSkills feature. |
| A personal skill name matches a project skill or the user's existing skill | The new personal skill is refused as a conflict | Choose a unique skill name. |
Key files
| File | Why to read it |
|---|---|
| apps/…/routes.ts | Inbound endpoint paths, authentication handoff, project selection, and request handling. |
| apps/…/auth.ts | Session, JWT, opaque-token, and deactivated-user checks. |
| apps/…/auth.ts | Local-JWKS verification and protected-resource metadata. |
| apps/…/env.ts | MCP resource URLs, public host behavior, audiences, and deployment defaults. |
| apps/…/public-auth-metadata.ts | Public-host discovery URL rewriting. |
| apps/…/server.ts | Conditional built-in tool groups and project context. |
| apps/…/register-mcp-tool.ts | Shared registration, schemas, logging, and error behavior. |
| apps/…/logging.ts | Per-call mode checks, usage tracking, and stored MCP call records. |
| apps/…/context-layer.ts | Context browsing, SQL and semantic queries, query result reuse, and story tool contracts. |
| apps/…/asset-tools.ts | Chart, map, and story tools exposed to MCP clients. |
| apps/…/chat-history.ts | Project chat search and transcript access for context administrators. |
| apps/…/tests.ts | Project test discovery, background runs, comparisons, results, and cancellation. |
| apps/…/context-admin-tools.ts | Role gate for project-wide observability tools. |
| apps/…/personal-skills.ts | MCP access, schemas, and role or group gating for personal skills. |
| apps/…/user-skill.service.ts | Personal skill ownership, name conflicts, and creation rules. |
| apps/…/user-group-features.ts | Shared role and user-group decision for personal skill access. |
| apps/…/custom-story-mcp.ts | Custom story files, drafts, publishing, query data, and access scope. |
| apps/…/run-agent-tool.ts | Project and chat context passed to agent-backed tools. |
| apps/…/ask-nao-runs.ts | Background ask_nao run states and cleanup. |
| apps/…/sub-agent.ts | ask_nao and polling behavior for long-running analytics work. |
| apps/…/helpers.ts | Chat and story access checks plus chart, map, and story result assembly. |
| apps/…/mcp.ts | Per-project external server configuration, discovery, enablement, validation, reload, and calls. |
| apps/…/mcp-oauth.ts | External-server OAuth discovery, client selection, token refresh, and storage flow. |
| apps/…/mcp.ts | External server configuration and project-visible server status contracts. |
| apps/…/mcp-openapi.ts | OpenAPI descriptions generated for discovered external tools. |
| apps/…/tool-schema-dialect.ts | Compatibility handling for JSON Schema dialect markers. |
| apps/…/ui-resources.ts | MCP Apps resource registration and client UI metadata. |
| apps/…/embed-tool-result.ts | Structured tool results, links, embeds, and fallback content. |