Connectors¶
A connector is a remote MCP server a user connects to their agent: Linear, a team
wiki, their own server. django-spec-agents (django_spec_agents.connectors, behind its
[connectors] extra) ships the model, the policy protocol, the URL policy and the
resolver that turns a caller's connectors into tools, each call asking the user unless
the tool is always allowed and the policy admits that. This
mount adds the per-run wiring, the editor, the authorization endpoints and what the
browser sees.
Install the extra the connections need:
What you bring¶
The libraries hold the shape and the safety; the secrets and the rules are yours.
- A model: a subclass of
AbstractConnectorwith your tenant relation, whoseconnector_policy()returns yourConnectorPolicy(visible,can_write,new_connector,can_always_allow). Personal and team connectors are a policy answer. - A
CredentialStore:get,saveandrevokea user's credential for a connector, andrevoke_allof a removed connector's.userisNonefor a connector's shared credential. Nothing ships, not even a development one. - An
AuthorizationFlow:begin(request, connector)returns the URL to send the user to, orNonewhen the credential is a form field (a pasted token);complete(request, connector)returns the credential from the callback (or the form).
Hand the three to the mount as a ConnectorMount:
from django_ag_ui import AGUIServer
from django_ag_ui.connectors import ConnectorMount
agent = AGUIServer(
registry,
connectors=ConnectorMount(
policy=Connector.connector_policy(),
store=EncryptedCredentialStore(),
flow=McpOAuthFlow(),
),
connector_editor=True,
)
django_ag_ui.connectors imports a model, so import it where the app registry is ready
(your urls.py), as you would django_spec_agents.connectors.
In a run¶
Every run resolves the caller's enabled connectors when it starts (off the event loop,
so the policy may query the database) and offers each one's allowed, approved tools as
<prefix>__<tool>, after the MCP bridge. The names the run already offers are claimed
first: the registry's, the bridge's, the specs', the agent's own toolsets', the client's
tools, read_attachment and render_a2ui; a connector tool of the same name is left out.
Every connector call asks. Each call ends the run on an approval interrupt unless the
tool is always allowed and both the policy's can_write and can_always_allow admit the
caller for that connector. A member who may use but not edit a team connector is asked on
every call, whatever an editor always-allowed. Only the person approves: an approval
reviewer's or a routine's answer is refused.
Where a call came from. A connector tool's TOOL_CALL_START and its approval
interrupt carry the connector under the connector metadata key
(django_ag_ui.connectors.CONNECTOR_FIELD):
{"type": "TOOL_CALL_START", "toolCallId": "c1", "toolCallName": "linear__search",
"metadata": {"connector": {"id": "7", "name": "Linear", "host": "mcp.linear.app",
"editable": false, "sharedCredential": false}}}
The interrupt in RUN_FINISHED's outcome carries the same object in its metadata, so
an approval card can say whose server is asking without a second lookup. editable is
the caller's can_write for that connector: "your connector" only when it is true, a
shared connector otherwise, whose card offers no "Always allow". sharedCredential says
the call connects with the connector's one credential rather than the caller's.
When connectors change. A run that saves or deletes a connector sends the
ag_ui.connectors_changed CUSTOM event (CONNECTORS_CHANGED_EVENT_NAME) with
{"id", "name"} once the write commits; the page refetches its list on it.
The editor¶
With connector_editor=True the mount serves the editor at connectors/. It lists and
touches only the connectors the caller sees and can_write admits; any other <id>
is a 404. Errors are {"error": ...}, with "field" naming the key that broke a rule.
| Request | Answer |
|---|---|
GET connectors/ |
{"connectors": [entry, ...]} |
POST connectors/ {"name", "url", "sharedCredential"?} |
the new entry, 201; a refused URL is 400 on url, a refused scope 403 |
PUT connectors/<id>/ {"name", "tools": [...]} |
the entry; records the reviewed tools |
PATCH connectors/<id>/ {"enabled": bool} |
the entry; off keeps the sign-in and the tool choices |
DELETE connectors/<id>/ |
204; revokes every credential it holds (each member's and the shared one), then deletes |
POST connectors/<id>/test/ |
the server's tools against the approved snapshot |
An entry is:
{"id": "7", "name": "Linear", "url": "https://mcp.linear.app/mcp", "host": "mcp.linear.app",
"enabled": true, "sharedCredential": false, "authorized": true, "allowedTools": ["search"],
"approvedTools": [{"name": "search", "schemaHash": "9f2c..."}],
"alwaysAllowedTools": []}
authorized says whether the store holds a credential for the caller (or the shared one);
the credential never leaves the store. Removing a connector calls django-spec-agents'
delete_connector, so your store's revoke_all forgets every member's credential and the
shared one before the row goes.
A URL cannot change. A PUT whose url differs from the stored one is a 400 on
url: stored credentials were issued for that server, and a new URL would send them
somewhere else. To point a connector at another URL, remove it and add it again; the
connectors panel offers exactly that.
Neither can a shared credential. sharedCredential (default false) is set at create:
your can_write sees it on the unsaved connector, so a host that forbids shared keys
refuses the create with a 403. A PUT that changes it is a 400 on sharedCredential,
because the stored credentials belong to the old scope; remove and add again.
The test connects as the caller, through the connector's URL policy and within its call deadline, and lists the tools a model would see. It turns nothing on:
{"tools": [{"name": "search", "description": "...", "inputSchema": {...},
"schemaHash": "9f2c...", "status": "approved", "allowed": true,
"alwaysAllowed": false, "canAlwaysAllow": true}],
"removed": ["archive"]}
status is approved (in the snapshot as listed), changed (in the snapshot under another
hash) or new; removed names approved tools the server no longer lists. A URL the policy
refuses at connect time is 422 with "status": "refused" and the reason; a server that
does not answer is 502 with "status": "unreachable" and no detail.
Recording a review. A PUT takes every tool the user reviewed as
{"name", "schemaHash", "allowed", "alwaysAllowed"}: each becomes the approved snapshot
(as the test hashed it), allowed ones the allow-list and alwaysAllowed ones the stored
always-allow answer. An alwaysAllowed tool the policy's can_always_allow refuses is a
403 on tools. A tool left out of the review leaves the run.
The editor's writes are CSRF-checked unless the mount passes csrf_exempt=True, and must
be application/json (else 415).
Authorizing¶
connectors/<id>/authorize/ and connectors/<id>/callback/ mount with connectors=, for
any connector the caller sees: a member authorizes a team connector they cannot edit,
and the credential saved is theirs. On a connector with a shared credential the credential
is the connector's, saved under user=None, and only a caller can_write admits may
connect or sign it out: anyone else is a 403 on each step.
GET authorize/redirects toflow.begin(...). WhenbeginreturnsNoneit is a400saying to post the credential.POST authorize/hands the request toflow.complete(...)and saves what it returns:{"id", "authorized": true}. It is CSRF-checked like the editor.DELETE authorize/signs out: it revokes the caller's credential (or the shared one) and answers204, deleting nothing else. It is CSRF-checked like the editor.GET callback/is where your flow's redirect lands: it callsflow.complete(...), saves the credential and answers a page that posts{"type": "ag_ui.connector_authorized", "id", "ok"}to its opener on the mount's own origin only, never"*", then closes. A failedcompleteanswersok: falsewith no detail. Checking the OAuthstateis the flow's job.
The connectors panel opens authorize/ in a popup and waits for that message.
The origin is ConnectorMount(callback_origin=) when you set it, else the callback
request's own: request.scheme and request.get_host(). When the page that opened the
popup is on another origin, the message is dropped and Connect never finishes. That happens
behind a dev proxy that rewrites the Host header (vite's changeOrigin: true) and behind
TLS termination without SECURE_PROXY_SSL_HEADER. Name the origin the browser sees:
It is one origin, http or https with a host and an optional valid port: "*", a path, a
query, a fragment or user info is refused with ImproperlyConfigured when the mount is
built. Without it, set USE_X_FORWARDED_HOST or SECURE_PROXY_SSL_HEADER behind a proxy.
Members: the read-only list¶
GET connectors/available/ mounts with connectors= alone, as authorize/ does, so a
host that sets connectors up itself (connector_editor=False) still lets members connect
and sign out. It lists every connector the policy's visible returns, disabled ones too:
{"connectors": [
{"id": "7", "name": "Linear", "host": "mcp.linear.app", "enabled": true,
"editable": false, "sharedCredential": false, "authorized": false,
"tools": ["search", "get_issue"]}
]}
A row the caller's can_write admits is the editor's entry with "editable": true. A
read-only row carries no url (a pasted URL can hold a key in its query string), nothing
to review, and tools: the names an editor allowed that are in the approved snapshot (a
run may offer fewer). authorized reads the caller's credential, or the shared one. The
editor's GET connectors/ is unchanged: it lists only what can_write admits.