MCP server and JSON APIs¶
Claude reaches Kosmos through a remote MCP server that Kosmos itself
serves at /mcp, behind an OAuth 2.1 authorization server that is also
Kosmos's own (apps/mcp). The tools call the same service functions as
the token-authenticated JSON APIs in apps/ai/api.py (under case/api/)
and apps/search/api.py. User-facing setup is in the operator guide:
Claude.
Access is per user. An MCP request carries a bearer token that a user
issued by signing in to Kosmos and clicking Allow; the tools run as that
user, so Claude sees exactly what the user can see, disconnecting under
Settings → Claude ends the tokens, and deactivating the user kills them
all. The JSON APIs keep their separate X-Kosmos-Token key
(ApiToken), shared with the LibreOffice companion; the MCP server does
not accept it.
Where the code is¶
| Module | Holds |
|---|---|
apps/mcp/server.py |
The MCP endpoint: bearer check, the JSON-RPC dispatch (initialize, ping, tools/list, tools/call), the 401 that starts sign-in |
apps/mcp/tools.py |
The fifteen tools: descriptions, JSON schemas, and the text each makes of a service function's result |
apps/mcp/oauth.py |
The authorization server: metadata documents, registration, the consent screen, the token and revocation endpoints, redirect-URI and PKCE checks |
apps/mcp/models.py |
McpClient, McpAuthorizationCode, McpConnection, McpToken |
apps/mcp/urls.py |
The well-known documents at the root, /mcp, /mcp/oauth/* |
templates/mcp/ |
The consent screen and the refusal page, on the sign-in pages' shell |
apps/accounts/api_auth.py |
kosmos_api_auth, the X-Kosmos-Token decorator, and ApiError, what every service function raises |
apps/matters/api.py |
The matters API (matters and proceedings as fields) and matters_api_auth, which takes a bearer token issued for that API |
apps/ai/api.py |
The service functions over a matter (list_matters, read_section, read_document, read_email_thread, search_materials, read_conversation, read_invoice, create_fact, create_witness, create_task, create_time_entry, create_event, complete_task, create_contact) and the JSON views over them |
apps/search/api.py |
search_practice() and its JSON view |
apps/settings/claude/views.py |
Settings → Claude: the connector address, the connections and Disconnect, the API token |
apps/ai/context.py |
The section formatters the matter API reuses |
Data model¶
McpClient: an OAuth client. Claude registers one dynamically (RFC 7591) when a connector is first set up, or identifies itself by a client ID metadata document, in which caseclient_idis the document's HTTPS URL and the row caches what it says (is_metadata_document).redirect_urisis what the consent screen will send the browser to;secret_hashis blank for a public client (Claude is one). A client has no access of its own.McpAuthorizationCode: the code the consent screen hands the browser, five minutes long, with the PKCE challenge and the redirect URI it was issued for. Deleted when redeemed.McpConnection: a user's standing authorization of a client, withcreated_atandlast_used_at. This is what Settings → Claude lists (themcpones) and Disconnect deletes; its tokens go with it (CASCADE).resourceis what the connection's tokens are for:mcp(the MCP server, the default) ormatters(the matters API; see The matters API's tokens).McpToken: one access token (an hour) and the refresh token that replaces it (ninety days). Refreshing marks the row's refresh token used and creates the next row; the old access token lives out its hour, so a request in flight is not failed by a refresh.
Tokens, codes and secrets are stored as SHA-256 digests. They are long random strings, so a digest is enough: a copy of the table cannot be replayed, and a presented token is looked up by hashing it.
How it works¶
Discovery and sign-in¶
- Claude posts to
/mcpwithout a token and gets401withWWW-Authenticate: Bearer resource_metadata="…/.well-known/oauth-protected-resource/mcp". The401is required: Claude ignores the header on any other status. - The protected resource metadata (RFC 9728) names the resource
(
https://host/mcp, which must equal the URL the user entered) and the authorization server (the site's base URL). - The authorization server metadata (RFC 8414) at
/.well-known/oauth-authorization-serverlists the endpoints and advertisesclient_id_metadata_document_supportedwithnoneamong the token endpoint auth methods. With both present Claude identifies itself by its published metadata document and registers nothing; without them it falls back to dynamic registration at/mcp/oauth/register, which is also offered. - The browser is sent to
/mcp/oauth/authorize. The view islogin_required, so an anonymous user goes through the ordinary two-step sign-in and comes back (next). The consent screen names the client and the host it will return to, and Allow issues a code.DailyDashCheckMiddlewareexempts/mcp/, or the once-a-day Dash redirect would swallow the flow. - Claude swaps the code at
/mcp/oauth/token(form-encoded) with its PKCE verifier, and the resource it named is checked against this server's MCP URL (RFC 8707). AMcpConnectionand its firstMcpTokenare made. Every later request carries the access token; a refresh rotates it.
Base addresses come from PUBLIC_BASE_URL when set, else the request.
There are no scopes: what a token reaches is its resource, and within
that what its user may reach. Every redirect carries iss (RFC 9207).
The matters API's tokens¶
The authorization server serves one resource besides the MCP server: the
matters API at /matters/api/ (apps/matters/api.py), which gives
matters and their proceedings as fields, read-only.
Kosmos Discover is its client. An
admin there clicks Connect; Discover reads the authorization server
metadata, registers itself (RFC 7591, a confidential client), and sends
the browser to /mcp/oauth/authorize with
resource=https://host/matters/api/. The consent screen says what that
grants (matters_only in the template), and the code, the connection
(McpConnection.resource = "matters") and every refresh stay on that
resource: resource_for() maps a resource parameter to mcp,
matters or nothing, the exchange and the refresh refuse to name a
different one, and McpToken.by_access_token(token, resource) finds a
token only at its own. So a Discover token is refused by /mcp and by
every other API, and a Claude token by /matters/api/.
A Discover has one connection: it registers afresh each time it connects,
so the installation is known by the origin of its redirect URI, and a new
matters connection from that origin deletes the earlier ones
(_replace_earlier_connections()), whoever allowed them.
matters_api_auth is the API's decorator: Authorization: Bearer with a
matters token, or the user's X-Kosmos-Token as on the other JSON APIs.
The connection is not Claude's, so Settings → Claude does not list it:
it is under Settings → Integrations, Kosmos Discover
(apps/settings/integrations/views.py, discover_disconnect), where the
user who allowed it sees it and an administrator sees every user's, and
Disconnect ends it.
Redirect URIs¶
A registered redirect URI must be HTTPS, or plain HTTP to the loopback
address (Claude Code listens on one). At authorization time the URI must
match a registered one exactly, except that a loopback URI matches on
any port (RFC 8252 section 7.3): Claude Code picks a free port per
session. An unknown client or an unregistered redirect ends on a page
(templates/mcp/error.html), never on a redirect the request chose;
every other error goes back to the client as an error parameter.
The endpoint¶
POST /mcp is streamable HTTP in its simplest form: one JSON-RPC message
in, one JSON object out (application/json), no event stream, no
session id. GET answers 405, as the transport provides for a server
with no stream to offer. Notifications answer 202 with no body. The
protocol versions accepted are in PROTOCOL_VERSIONS; an
MCP-Protocol-Version header outside them is 400. A browser-sent
Origin that is not this host is 403 (the transport's DNS-rebinding
guard).
Tool results are text. A refusal from a service function (ApiError)
comes back as a tool result with isError: true and the message
prefixed Kosmos API error (<status>), so the model learns what was
wrong (a 404 it can retry with another id, a 403 it cannot). Arguments
that do not fit the schema are a JSON-RPC -32602.
Tools¶
find_matter, read_matter, search_matter, search_kosmos,
read_document, read_email_thread, read_conversation,
read_invoice, add_fact, add_witness, add_task, add_time_entry,
add_event, complete_task, add_contact. Long reads
truncate at READ_TRUNCATE_CHARS (40k) with an explicit marker. All
intelligence lives in the service functions; tools.py only describes
and formats. initialize also returns instructions telling the model
to resolve a matter once and read the overview first. The contacts and
tasks sections are manifests with [contact:ID] and [task:ID]
handles (the contacts section also names the groups and roles
add_contact may use on the matter); they are formatted in api.py,
not shared with apps/ai/context.py.
Access¶
Matters are the user's accessible open matters only, through
filter_matters_for_user(), and every denial of a matter, document,
thread or conversation is a 404 so it does not confirm existence. The
money sections (FINANCIAL_SECTIONS: rates, ledger, trust) and invoice
reads additionally need has_financial_access() (admin or
perm_financial), like the in-app Rates and Ledger tabs; that denial is
a 403, since the matter's existence is already known to the caller. The
activity section is open to every user who can see the matter, as the
Activity screens are. Intakes in the practice-wide search need
perm_intakes. Inactive users are refused at the token check, on both
the bearer and the X-Kosmos-Token paths. See the
permissions reference.
Write access¶
Seven writes, none destructive. Six create a row; the seventh marks a task complete, which the tasks page can undo. Nothing on the money side (invoices, payments, trust) and nothing that sends mail is reachable. The record is Claude's writes stay create-shaped.
- Timeline facts and witnesses:
create_factandcreate_witnessreuse_create_fact_from_entry()and_create_witness_from_entry()(validation, matter scoping of cited ids, name dedup): exactly what the in-app chat's fenced blocks can do, no more. - Tasks:
create_taskreusescreate_task_from_ai_entry(), assigned to the acting user; the matter is authorized here, because the service resolves by name without an access check, and the task is re-pointed after creation in case a duplicate name resolved elsewhere.complete_tasksetsSTATUS_COMPLETEthrough the model'ssave(), which stampsdate_completed; the checklist guard (can_complete_task()) applies as on the tasks page, a task already complete is reported rather than failed, and a task on a matter the user cannot see is a 404. - Time entries:
create_time_entrytakes a narrative, hours and an optional date and category (one of the matter'sActivityCategoryrows, by name). The matter must be hourly, as the time form requires. The rate iscalculate_rate_for_matter()for the acting user, never an argument; the entry is unbilled (invoicenull,enteredandcompfalse) and flaggedneeds_approval, which the Activity tab's Approve action clears (see Time and billing). Abbreviation codes are not expanded: the model writes the narrative in full. - Events:
create_eventapplies the event form's rules (a start with no end runs an hour viadefault_end_time(), an end date on the start date is dropped, times must run forward within a day) and saves the eventPendingso the calendar's default filter shows it. The matter is optional, as on the form; without one the event is the firm's. No assignee unlessassigned_to_me, which matches how nearly every event in the firm's calendar is kept. - Contacts:
create_contactmakes one address-book row, name-deduped (a same-named contact is reported and its details left alone), with the contact form's normalisation (normalize_phone(), lower-cased email). Withmatter_idit also puts the contact on the matter as aRelationship, which needs arolefromassignable_roles()and agroupfrom the matter's groups less the system Client group; both are resolved before anything is created, so a bad name leaves no orphan, and a refusal lists the names to choose from.already_assigned()stops a duplicate party row.
Things that bite¶
- The formatters are shared on purpose. A change to a section
formatter in
apps/ai/context.pychanges what the in-app chat, Claude and the JSON API see; a new section needs aSECTIONSentry and, if it carries money, a place inFINANCIAL_SECTIONS, and theenuminread_matter's schema followsSECTIONSautomatically. - Matter scope differs by API. The matter functions serve open matters only; the practice-wide search lists matters of any status, because it is navigational.
- The
resourcemust match the URL the user typed. The protected resource metadata sayshttps://host/mcp; a proxy that changes the host, or a user who types a different one, breaks the token exchange.PUBLIC_BASE_URLpins it. - Registration is open. It has to be: Claude registers before any user is involved. A client has no access until a user allows it, so the only thing registration can do is make rows; it is rate-limited per IP and rows nobody connected within a day are swept on the next registration.
- Refresh reuse inside sixty seconds is tolerated. Two of Claude's
workers can refresh the same token at once; within
REFRESH_REUSE_GRACEthe second gets its own pair. Later reuse is treated as theft and revokes the connection (OAuth 2.1 section 4.3.1). - Email threads list most-recent-activity first (2026-09-25), so the
40k truncation drops the oldest; keep that order when touching
_format_email_threads().
How this replaced the per-user stdio script is recorded in Claude connects over a remote MCP server with OAuth.
Related¶
- Claude for setup; The archive and the intake chat for the matter tools and the search handler this reuses; Drafts for the companion that shares the API token.