Model Context Protocol (MCP) server

MCP Beta

See also

The MCP server is one of Pharaoh’s surfaces.

The Model Context Protocol (MCP) server gives an AI assistant your project’s needs graph and references, and the public sphinx-needs and ubCode documentation, through the nine tools below.

The MCP server starts automatically when the ubcode extension is activated. You can check its status within the command palette by searching for “MCP: List Servers”, or in the copilot window. See the VS Code MCP documentation for more details on using MCP servers.

By default, the MCP uses the root of your project workspace to retrieve cached information. This can be configured though, using the ubcode.mcp.defaultConfig VS Code setting. When the assistant connects, the server tells it whether that ubproject.toml exists. If it does, the project tools read that project unless a call names a file in another one. If it does not, as in a repository holding several projects with none at its root, the assistant is told to name a file of the project in every project tool call.

Each call re-checks the project for files changed since the previous one, so an answer reflects the files as saved (see Keeping the index in memory below).

Tools

The server offers nine tools. The seven project tools read your project’s data and need an active license for that project; the two documentation tools need none (see The MCP server).

Tool

What it does

License

get_graph_schema

Returns the needs graph’s schema: node labels (need types), relationship types (link types), node properties and the query parameters available.

required

query_cypher

Runs a read-only openCypher query over the needs graph.

required

search_references

Searches everything the project can cross-reference (its labels, glossary terms and documents, and every loaded intersphinx inventory).

required

resolve_reference

Says whether one reference would resolve, and to what, and can list its usages.

required

untraced_symbols

Lists the functions and methods of the codelinks source files that trace to no need, with the coverage totals and what the syntactic inventory could not see, for a project that writes a [codelinks_coverage] table (alpha, see Finding code that traces to no need).

required

requirements_coverage

Reports, per requirement, the functions its passing tests executed, from per-test coverage files a test run wrote, with the traced functions they missed and untraced ones to review (alpha, see Checking that each requirement’s tests run its code).

required

project_diagnostics

Reports what ubc check reports for the project (see Linting on the command line): the findings counted by grade and by code, whether ubc check would pass, and the findings most severe first, with the project’s overview (needs by type and status, and links). It takes the path, code, severity and need filters and a limit; a path folder holding none of the project’s indexed documents reports no findings, as ubc check <path> does, except under a [source] dir, where ubc check anchors exclude and variant patterns at the configuration folder.

required

search_docs

Searches the public sphinx-needs and ubCode documentation.

not needed

fetch_doc_page

Fetches one documentation page found by search_docs.

not needed

Each tool’s parameters and annotations, and the full description the model is given, are on the MCP tools reference.

When the index could not read a source (a page that is not UTF-8, say), the needs defined in it are missing from the graph. The seven project tools still answer over the needs that remain, and do not fail: each answer names the missing sources in an unreadable_sources list (unreadableSources for query_cypher and project_diagnostics), each with its path, line, code and message, and closes its text with one warning line (project_diagnostics names each source’s reason in it), so the model can tell a need that is missing from one that does not exist. An assume_unchanged answer names the same sources as the pass it is served from. When a source cannot be read lists which findings count.

Serving fewer tools

ubc serve mcp --tools <names> serves only the tools it names, as a comma-separated list, for example --tools query_cypher,get_graph_schema. The other tools are left out of the tool list, and a call to one of them is refused as an unknown tool. A name that is not one of the tools above, or an empty list, stops the server at startup with the reason, so a mistyped name cannot leave a tool out unnoticed. Without --tools every tool is served.

The flag applies where you launch or register ubc serve mcp yourself; the MCP server the VS Code extension starts serves every tool.

Views in the chat (MCP Apps)

MCP Experimental

To a client that declares MCP Apps support (Claude Desktop, and VS Code’s Copilot Chat), the server offers three more tools, which the assistant calls to show you something:

  • show_needs (titled “Needs explorer”) opens the needs explorer on the needs a query returns: a table to sort and filter, and each need’s fields, links, trace graph and ubc check findings. Asked to open one need (“Open REQ_AUTH in the needs explorer”), the assistant names it as open: the card’s first button then opens that need, and full screen (or a host with no full screen) shows it at once.

  • show_health (titled “Health: ubc check findings”) opens the Health view: what ubc check reports for the project, with the project’s overview; a finding about a need opens that need.

  • need_graph answers one need’s trace neighbourhood: the needs within a few links of it, every link between them, its links to ids that are no need, and, for each need in it, how many needs outside the graph its links lead to, each way and per link type. The explorer draws it, and the assistant reads it.

Each view opens in the chat as a compact card, with a few numbers, its first rows and two buttons; the first opens it full screen. A host that offers no full screen shows the whole view in the chat.

The Health view's card in the chat: 0 errors, 2 warnings, 0 info and 22 needs checked, the two warnings, and the Open findings and Ask in chat buttons

The Health view’s card in Claude Desktop’s chat, for a project whose ubc check would fail on two warnings

A need’s detail shows its findings, links, fields and trace graph as sections you open and close, and keeps the ones you open as you move from need to need.

The need TEST_SESSION_FLOW, opened from its dead-link finding in the Health view: its finding, links and fields beside its trace graph, where its link to SPEC_BILLING ends at a dashed "no such need" card

A finding opens its need: TEST_SESSION_FLOW links to SPEC_BILLING, an id no need has, so the graph draws that link dashed, to a “no such need” card

In the trace graph, a need with links to needs the graph does not show carries a “+N” pill at the end of a short dotted arrow, one for its own links and one for the links to it: N needs are not shown, and the arrow points the way those links do. Often a larger depth does not draw them either: the graph follows links one way from the need it is centred on, so a need it reaches can link to needs beside it. Hold the pointer over a pill to see the link types; click it, or its need, to open that need: its graph draws them, within its caps. The need the graph is centred on has a pill only when its graph was capped, and that pill opens nothing. The Graph’s header counts the needs with such links. The trace graph is laid out top to bottom or left to right, whichever shows it larger, as the VS Code Needs graph view lays it out (Layout and zoom): a need with more than 6 needs linked to it by one link type, in one direction, and to nothing else, draws them as a block of columns under one link name, when that shows the graph larger (on a graph of more than 400 links, not counting a need’s links to itself, as far as an estimate can tell). It opens fitted, or at a readable zoom around the need when the whole graph would be too small to read; Fit shows the whole graph. On a wide full screen the list sits beside the open need; on a narrower one, or in the chat, an open need takes the whole view, and the breadcrumb above its title (“Needs explorer ›” or “Health ›”), or Esc, returns to the list.

The needs explorer full screen: the list of 22 needs beside the open need REQ_CHG_START, with its incoming links and a trace graph from the requirement through three specifications to their tests

The needs explorer full screen on a wide window: the list beside the open need, with its links and trace graph

Each view links to the other through the chat: the link writes the request for you to send.

A client that does not declare MCP Apps support gets the nine tools above, unchanged. A client that declares it but draws no view still gets the three tools, and their text answers are complete. The views read your project only through the project tools, so they need a license as those do. When a call fails (no license for the project, a project that cannot be loaded, an argument it refuses), the tool answers with its message. Where the host draws the view, the view shows that message in place of the list or of a need’s links and fields, and in the Graph’s header for a graph. Whether a host draws a view at all for a call that failed is the host’s choice.

Turning the views off

In VS Code, set ubcode.mcp.apps to off (the default is auto): the MCP server the extension starts then offers no views, and answers as it does a client that does not declare MCP Apps support, with the tools that answer in text. VS Code applies the change when it restarts the ubCode MCP server; restart it from MCP: List Servers if it does not.

Keeping the index in memory

The graph tools (get_graph_schema and query_cypher) keep each project’s needs index in memory between calls. Every call still re-scans the project for changed files, by their modification time, and re-indexes what changed, so an answer reflects the files as saved; what a project kept in memory skips is reloading the whole index cache from disk on every call. The one exception is a call that passes assume_unchanged, which skips the re-scan too (see Skipping the re-scan: assume_unchanged below).

Who gains, and when:

  • Repeated graph queries with no edit in between are answered from memory (about 3.5x faster at 50,000–200,000 needs).

  • The first graph query after an edit gains only if the MCP server is the first process to index that edit. In VS Code the language server re-indexes on save (server.index_on_save, on by default) and writes the cache, so the MCP server’s next call reloads it once, as every call did before; the queries after that are answered from memory again. The same happens inside the MCP server when a search_references or resolve_reference call indexes an edit before the next graph query.

  • MCP clients with no language server running on the project gain on the first query after an edit too.

ubc serve mcp --projects-in-memory N sets how many projects stay in memory at once (default 2). When a further project is queried, the least recently used one is unloaded and its next call reloads from disk. Each project kept in memory holds a full copy of its index (the whole server measured about 0.8 GB with one 200,000-need project in memory), so on very large projects lower it to 1 to save memory, or set it to 0 to reload the index on every call.

A project with no graph query for 10 minutes is unloaded: the server drops the needs index it kept in memory for the graph tools, so a session left open with no agent at work gives that memory back; its next graph query reloads it from disk once. The separate index that search_references and resolve_reference keep is not unloaded. ubc serve mcp --unload-after-idle-minutes N sets the timeout in minutes (default 10); 0 keeps a project in memory until another project displaces it. How much of the unloaded memory the operating system sees returned depends on the memory allocator. On Linux, in one run per case, the server’s memory use dropped by 7% to 26%: about 210 MB at 200,000 needs (in one run from about 820 MB to about 610 MB), and about 27 MB at 50,000 needs. It does not fall back to the server’s starting size, because the allocator keeps freed memory for reuse by later queries.

These flags apply where you launch or register ubc serve mcp yourself, for example in the MCP configuration that ubc agent install --register-mcp writes for an agent harness. The MCP server the VS Code extension starts keeps one project in memory (--projects-in-memory 1), since the language server already holds an index of the same workspace, and unloads it after the default 10 idle minutes.

Because a project kept in memory keeps its in-session state between calls, as the language server does, a remote source that failed to download (an external needs.json or needimport URL, or an intersphinx inventory) is retried after a backoff of 5 minutes rather than on every call. Restart the MCP server to retry at once, or set --projects-in-memory 0 to retry on every call.

Skipping the re-scan: assume_unchanged

An agent that runs many graph queries over a project nobody is editing can pass assume_unchanged: true to query_cypher or get_graph_schema. It is the agent’s promise that nothing in the project changed since its last graph query without the flag. The server then answers from the index it holds in memory, without re-checking a single file, and says how old that index is:

  • the structured answer gains an index field, with assumedUnchanged, indexedAt (no later than the start of the re-scan that built the index, in UTC) and ageSeconds (spelled assumed_unchanged, indexed_at and age_seconds in get_graph_schema’s answer);

  • a query_cypher table ends with a note: line saying the same.

An answer given this way may not reflect edits saved after indexedAt. Calls without the flag re-check the files as before, and their answers carry no index field. A call without the flag on the same project does wait for calls with it that are still running. The tool descriptions tell the agent to make the first call of a burst without the flag, and never to pass it on the first call after an edit, its own or anyone else’s. It is also how an agent pages through one large result over the index in memory.

The flag never makes a call fail because the project is not in memory, and never answers from the wrong project. When the project is not in memory (its first call, after it was unloaded, or with --projects-in-memory 0), or its ubproject.toml changed, the call re-scans as any call does, and its answer says so with assumedUnchanged: false. A configuration edit is therefore always seen. An answer from memory writes nothing to disk, and counts as a use of the project for the idle unload.

What it saves is the re-scan and, for query_cypher, the graph each query otherwise builds from the index: the first query with the flag builds that graph once and the following ones reuse it, until the next re-scan of the project (a call without the flag, or a call with it that had to re-check) drops it, or the project is unloaded. Mixing calls with and without the flag therefore rebuilds the graph on the first call with the flag after each one without it. With --unload-after-idle-minutes 0 and only calls with the flag, the index and its graph stay in memory indefinitely, and the age the answers report keeps growing. While it is kept, the graph adds to the server’s memory for that project (measured on Linux at 200,000 needs: about 16 MB for a graph of the needs alone and 32 MB for the full graph, on a server of about 375 MB; about 9 to 24 MB more than a server without the flag after the same calls, with no higher peak); a server whose clients never pass the flag keeps none. After a query that follows links, a query over the needs alone runs on the kept full graph, which is about 10% slower, so such a query close to the 30-second query time limit can time out with the flag where it would not without it. get_graph_schema does not use that graph, so with the flag it saves only the re-scan.