MCP and external API surface
Nao exposes analytics, context, story, chat review, context-test, and personal-skill capabilities through a Model Context Protocol (MCP) server. AI clients connect to a project-scoped endpoint and act as an authenticated Nao user. Nao also connects to approved external MCP services so its own agents can use outside tools.
External MCP endpoint
The external MCP endpoint is the main interface for AI clients such as desktop assistants and automation tools. It provides a stateless Streamable HTTP connection at /mcp and, when a project is named, at /mcp/:projectId.
Endpoint access
The endpoint accepts MCP requests over POST. A client can use the bare path when its user has one clear project or include the project ID when it must select a project. The request must identify a signed-in and active Nao user who has access to the project with a role above viewer.
The endpoint is a project capability, not a public anonymous API. An administrator must enable it for the project. Requests also pass the deployment's cloud billing check before Nao creates the MCP server.
References: apps/…/routes.ts• L24-45 apps/…/routes.ts• L47-79 apps/…/mcp-endpoint.ts• L6-17
Project selection
Project selection binds every MCP call to one Nao workspace. The bare endpoint resolves the project from the user's available projects. The project path makes the choice explicit for clients that work with more than one project.
The selected project controls the data sources, context files, stories, feature flags, and user-group access that the client can reach. A client cannot use a chat or story from another project through the same MCP identity. Chat-linked actions also require the chat to belong to the authenticated user and selected project.
References: apps/…/routes.ts• L55-70 apps/…/project.ts• L1-120 apps/…/run-agent-tool.ts• L9-25
Authentication boundary
MCP authentication maps an incoming client request to a Nao user before any tool is exposed. The boundary accepts an existing Nao session for browser-based use and bearer access tokens for external clients.
Bearer authentication
External clients send Authorization: Bearer <token>. Nao supports JWT access tokens issued by its auth service and stored opaque tokens. It checks token validity, expiry, audience, issuer, and user status before it accepts the request.
Sessions that still require two-factor setup do not pass the MCP boundary. Deactivated users also lose access even when a token can be found or verified.
When a deployment uses a separate public MCP host, the public /mcp URL is included in token audiences and protected-resource metadata. This lets an external client complete OAuth discovery against the host it uses to connect.
References: apps/…/auth.ts• L13-27 apps/…/auth.ts• L30-69 apps/…/auth.ts• L99-128 apps/…/env.ts• L382-393
MCP OAuth clients
Self-hosted administrators can create OAuth clients for external MCP consumers. A confidential client receives a secret for server-to-server use. A public client uses PKCE and does not receive a secret. The secret is returned only when the client is created.
This client management surface is deployment-wide on self-hosted installations. It is not available in multi-tenant cloud because one shared client store could expose clients owned by other projects.
References: apps/…/mcp-oauth-clients.routes.ts• L11-25 apps/…/mcp-oauth-clients.routes.ts• L27-32 apps/…/mcp-oauth-clients.routes.ts• L57-99
MCP tool surface
The MCP server builds its tool list for the authenticated user and project at connection time. The list describes the actions that Nao permits in the current project. It does not expose every product feature to every caller.
Agent mode
Agent mode gives a client a high-level way to ask Nao to perform analytics work. ask_nao sends a natural-language task through the same agent capability used by the Nao chat experience. The run can answer a question, create or update stories when allowed, or ask the user for clarification.
Long work returns a running status with a chat ID. The client then calls get_nao_answer until the run is complete. A completed response includes the answer plus query and story references. A clarification response keeps the same chat ID so the client can send the user's answer in context.
Nao keeps the run state only as a short-lived lookup. If the process loses that state, it can recover the answer and stories from the saved chat, but query summaries may not be available. A chat ID supplied to an agent-backed tool is used only when it belongs to the calling user and project; otherwise the tool runs with project-scoped context outside a chat.
References: apps/…/sub-agent.ts• L37-81 apps/…/sub-agent.ts• L129-243 apps/…/sub-agent.ts• L331-368 apps/…/ask-nao-runs.ts• L42-96 apps/…/run-agent-tool.ts• L9-25
Context mode
Context mode gives the client direct control over smaller workflow steps. It can browse project context, search and read files, run SQL or governed semantic queries, and create or update stories.
The available query type follows project data settings. Raw warehouse SQL may be unavailable while governed semantic queries remain available. Query calls can include a chat ID, and successful calls return a reusable query_id for later chart or story use. A query linked to a chat must use data from that chat.
Story tools support classic markdown stories and, when enabled, custom stories that publish interactive applications. Classic story updates replace the full content when content is supplied. Custom stories use source files and file deletions instead of markdown content. Standalone stories can use saved query data or rows supplied by the request.
References: apps/…/context-layer.ts• L46-80 apps/…/context-layer.ts• L100-136 apps/…/context-layer.ts• L168-245 apps/…/context-layer.ts• L301-469
Visualization and story lifecycle
Nao turns query results into client-consumable visual assets and provides the basic story lifecycle around them. display_chart and display_map use a reusable query_id and return structured metadata, an embed URL when persistence succeeds, and a markdown block for classic stories.
Story management also supports listing visible stories, reading the latest version, archiving a story for recovery, and permanently deleting a story and its versions. Clients must use the UUID returned by Nao for story operations rather than a story slug. Archive is reversible in the Nao UI; delete is permanent.
References: apps/…/asset-tools.ts• L36-89 apps/…/asset-tools.ts• L91-171 apps/…/asset-tools.ts• L202-266 apps/…/asset-tools.ts• L281-395
Context administration mode
Context administration mode adds project-wide review and evaluation tools for users with the admin or context_admin role. These tools let an authorized client search and read project chat history, inspect tool errors and feedback, list context tests, start and monitor test runs, compare production with context changes, and cancel queued or active runs.
Chat search returns paged summaries. Filters include author, source, activity date, feedback, message count, and tool errors. get_chat returns the transcript and tool calls, while tool outputs are optional and size-limited. Test runs execute in the background, so the caller uses the returned run ID with get_test_run. A project runs one test run at a time.
Test runs can use production context, context changes, or both for comparison. A comparison reports whether each test regressed, worsened, fixed, improved, appeared, was removed, stayed unchanged, or remains pending.
References: apps/…/context-admin-tools.ts• L10-27 apps/…/chat-history.ts• L19-32 apps/…/chat-history.ts• L97-223 apps/…/tests.ts• L20-73 apps/…/tests.ts• L128-266
Personal skill mode
Personal skill mode lets an authorized user save instructions for one kind of recurring request. The skill is available in the user's later Nao chats through its description or a /<name> command, and it remains private to that user while its share status is private.
The client can list the user's personal skills or create one with a lowercase name, a use description, and Markdown instructions. Administrators and context administrators can manage personal skills; regular members need the personal-skills user-group feature. A name already used by the user or a project skill is refused.
References: apps/…/server.ts• L54-57 apps/…/personal-skills.ts• L12-27 apps/…/personal-skills.ts• L29-40 apps/…/personal-skills.ts• L43-95
Custom story interface
Custom stories let an AI client create or revise an interactive story app through the same story tools used for classic stories. The client sends app source as files and can receive a starter draft when it omits those files. Later edits use file changes and optional deletions rather than markdown content.
The story app reads query results by query ID. A chat-linked app uses queries from its Nao chat, while a standalone app uses saved query data or rows supplied with the request. Nao holds a write lock while it changes a custom story and publishes a new version only after the app builds successfully. Build errors return to the client and do not replace the published version.
References: apps/…/custom-story-mcp.ts• L39-61 apps/…/custom-story-mcp.ts• L63-85 apps/…/custom-story-mcp.ts• L87-191 apps/…/custom-story-mcp.ts• L247-297
Tool result contract
Nao returns both readable content and structured content when a tool supports it. A human-facing client can show the message while an integration reads fields such as status, query IDs, story IDs, URLs, model names, and clarification questions.
Tool inputs and outputs carry schemas. Nao validates inputs before it runs the action and returns an MCP error result when the action fails. Clients should use the declared result status instead of treating every response as a final text answer. Tool calls also produce project- and user-scoped audit records with success and duration information.
References: apps/…/register-mcp-tool.ts• L35-87 apps/…/sub-agent.ts• L154-182 apps/…/sub-agent.ts• L393-513 apps/…/logging.ts• L22-33 apps/…/logging.ts• L53-89
Access controls
Access controls apply at several levels so a connected client cannot bypass Nao's project policy. The endpoint checks project membership and role first. Tool registration then applies endpoint mode, user-group features, project settings, story permissions, and the role gate for context administration and personal-skill tools.
An administrator can enable or disable endpoint modes and manage the project feature boundary. The resulting MCP session is narrower than the full Nao product surface when the project or user group does not grant a capability. Each tool call checks its mode before the action runs.
References: apps/…/routes.ts• L62-87 apps/…/server.ts• L31-57 apps/…/context-admin-tools.ts• L10-27 apps/…/personal-skills.ts• L29-40 apps/…/mcp-endpoint.routes.ts• L13-28 apps/…/user-group-feature-access.service.ts• L124-142 apps/…/logging.ts• L35-61 apps/…/logging.ts• L78-110
Outbound MCP integrations
Nao also acts as an MCP client for services configured in a project. These integrations add approved external tools to the Nao agent rather than to the external client's MCP session.
Server connections
Project MCP server definitions support HTTP transports and local standard-input/output transports where the deployment permits them. Cloud deployments allow HTTP connections only. Nao discovers each remote tool and its input schema, then calls the service only when an agent selects that tool.
This on-demand model keeps external tools out of the agent context until they are needed. Project administrators can disable a whole server or an individual tool. User groups can grant a smaller set of servers and tools.
References: apps/…/mcp.ts• L126-133 apps/…/mcp.ts• L217-248 apps/…/mcp.ts• L336-409 apps/…/mcp.ts• L1034-1041
External service authentication
Outbound connections support static headers and per-user OAuth. Static credentials let a configured service use one shared connection. OAuth keeps a separate connection for each Nao user and refreshes or invalidates that user's token as needed.
When a user must connect an OAuth service, Nao starts an authorization flow from the Nao UI and discovers the service's tools after the callback succeeds. A missing or expired connection blocks the tool call instead of silently using another user's credentials. Inbound OAuth token responses remain portable when providers return both relative and absolute expiry fields.
References: apps/…/mcp-oauth.ts• L49-56 apps/…/mcp-oauth.ts• L70-117 apps/…/mcp-oauth.ts• L125-195 apps/…/mcp.ts• L500-532 apps/…/auth.ts• L318-329 apps/…/token-response.ts• L5-19
For setup and the detailed tool catalog, see MCP server, authentication, and tools.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep endpoint paths, project-resolution rules, public URL metadata, and token audiences aligned.
- Keep session checks, bearer-token checks, two-factor setup handling, and deactivated-user handling aligned.
- Keep endpoint mode settings aligned with per-call mode checks, tool registration rules, and access descriptions sent to clients.
- Keep
ask_naostatus values aligned with polling, clarification reuse, saved-chat recovery, and short-lived run cleanup. - Keep model selection descriptions aligned with the models that the project can use.
- Keep query IDs and story IDs usable across query, chart, map, story, and chat-linking tools.
- Keep query chat association and local-versus-warehouse SQL rules aligned with the schemas exposed to clients.
- Keep classic story content separate from custom story source files. Keep custom story query-data rules aligned for chat-linked and standalone stories.
- Keep custom story edit locks, draft state, build results, and published versions aligned so failed writes do not expose an incomplete app.
- Keep context-admin and personal-skill role and user-group checks aligned with tool registration.
- Keep test-run modes, single-run limits, background polling, comparison results, and cancellation behavior aligned across the test tools.
- Keep outbound server, tool, and user-group checks aligned so discovery cannot expose a tool that a later call rejects.
- Keep OAuth token responses portable when providers return both relative and absolute expiry fields.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| MCP endpoint is disabled for the project | The client receives a service-unavailable response and no tools are created | Enable the endpoint in project MCP settings |
| Token is missing, expired, or has the wrong audience | The request is rejected before project selection | Use a valid Nao session or bearer token for the MCP URL |
| Session still requires two-factor setup or the user is deactivated | Authentication fails even when the session or token exists | Complete two-factor setup and use an active user |
| User is a viewer or is not a member of the selected project | The client cannot open an MCP session | Grant project access with a permitted role |
| A non-admin user calls context administration tools | Chat history and test tools are absent from the session | Use an admin or context_admin account |
| A member lacks the personal-skills feature | Personal-skill tools are absent from the session | Grant the personal-skills user-group feature |
| A personal skill name is already used by the user or project | Skill creation is rejected | List personal skills first and choose an unused name |
ask_nao exceeds the client request budget | The client gets running instead of a final answer | Poll get_nao_answer with the returned chat ID |
| The in-memory run entry expires before polling | Nao can recover the answer from saved chat, but query summaries may be missing | Poll soon and retain the chat ID |
ask_nao returns needs_clarification | The client cannot complete the task with the current answer | Relay the question and call ask_nao again with the same chat ID |
| Requested model is not available to the project | The agent run fails before it starts | Use a model from the session's available model description or omit the model |
| A query ID has no result available to the caller or its chart/map keys do not match result columns | The visualization call returns an MCP error | Re-run the query and use its returned ID and column names |
| Custom story files fail to build | No new custom story version is published, and the client receives build errors | Fix the returned files and call update_story again |
| A custom story write overlaps another MCP write | The later call is rejected and the story is not partially merged | Wait for the active write to finish, then retry |
A chat-linked custom story includes query_data | The request is rejected because the app must use queries from its chat | Omit query_data and use query IDs from the linked chat |
| A test run is still active when another run starts | The new run cannot proceed because each project runs one at a time | Poll or cancel the active run before starting another |
| A test path or tag selects no valid test | The run cannot cover the requested selection | Use paths and tags returned by list_tests |
| OAuth connection for an outbound server is missing or invalid | Discovery or the tool call stops and asks for a user connection | Connect the service again from Nao |
| OAuth token response contains provider-specific absolute expiry alongside relative expiry | Some MCP clients reject or mishandle the token response | Return the portable response with the standard relative expiry |
| Project policy disables a server or tool | The tool is absent or rejected even when the remote service is healthy | Review project and user-group MCP access |
Key files
| File | Why to read it |
|---|---|
| apps/…/routes.ts | Defines the inbound MCP endpoint, request methods, project binding, and disabled-endpoint behavior. |
| apps/…/auth.ts | Shows how sessions and bearer tokens become an authenticated Nao user. |
| apps/…/server.ts | Shows how endpoint settings, user features, roles, and personal-skill access shape the MCP tool surface. |
| apps/…/logging.ts | Shows per-call mode enforcement, structured result handling, and audit tracking. |
| apps/…/sub-agent.ts | Defines the high-level analytics flow, model choice, clarification, and asynchronous answer contract. |
| apps/…/ask-nao-runs.ts | Defines the short-lived state used while asynchronous agent runs continue after the request ends. |
| apps/…/run-agent-tool.ts | Defines how direct agent tools stay inside the caller's project and permitted chat. |
| apps/…/context-layer.ts | Defines direct context, query, and classic story capabilities. |
| apps/…/asset-tools.ts | Defines chart, map, story lookup, archive, and delete capabilities. |
| apps/…/chat-history.ts | Defines project chat search, transcript results, and bounded tool-call details for authorized administrators. |
| apps/…/tests.ts | Defines test discovery, background runs, production-versus-change comparisons, polling, and cancellation. |
| apps/…/context-admin-tools.ts | Defines the role boundary for chat, test, and recommendation administration tools. |
| apps/…/personal-skills.ts | Defines the personal-skill list and create actions exposed to permitted MCP users. |
| apps/…/token-response.ts | Keeps inbound OAuth token responses portable for external MCP clients. |
| apps/…/custom-story-mcp.ts | Defines the external contract for creating, reading, updating, locking, and publishing interactive story apps. |
| apps/…/helpers.ts | Defines project-safe story lookup, query-result reuse, and client preview results for visual tools. |
| apps/…/mcp.ts | Explains how Nao discovers and calls outbound MCP services. |
| apps/…/mcp-oauth.ts | Explains per-user OAuth for outbound MCP services. |
| apps/…/mcp-oauth-clients.routes.ts | Defines self-hosted OAuth client management for inbound MCP consumers. |