- Wire Cursor adapter and provider session handling into orchestration v2 - Add replay fixtures, recording script, and coverage for Cursor turn flows - Update docs and debug timeline mapping for Cursor events
305 lines
9.8 KiB
Markdown
305 lines
9.8 KiB
Markdown
# Orchestrator MCP Server
|
|
|
|
## Purpose
|
|
|
|
T3 exposes V2 orchestration through its app-owned MCP endpoint. A provider
|
|
agent can use this endpoint to:
|
|
|
|
- create an app-owned sub-agent on any supported provider instance;
|
|
- wait for or poll the sub-agent's durable result;
|
|
- cancel an active delegated task; and
|
|
- create one or more ordinary top-level T3 threads.
|
|
|
|
These are T3 orchestration operations, not provider-native sub-agent APIs.
|
|
Delegated tasks always create a T3 child thread and run. The child receives
|
|
only the supplied task prompt, plus an optional role instruction supplied in
|
|
the same tool call. Parent conversation history is not copied into the child.
|
|
|
|
## Transport And Authentication
|
|
|
|
The orchestration tools share the existing authenticated HTTP MCP endpoint:
|
|
|
|
```text
|
|
http://127.0.0.1:<server-port>/mcp
|
|
```
|
|
|
|
The provider-visible server key is `t3-code`. The endpoint registers both the
|
|
preview toolkit and the orchestration toolkit.
|
|
|
|
Before `ProviderSessionManager` opens a new V2 provider session, it asks
|
|
`McpSessionRegistry` for a credential scoped to:
|
|
|
|
- the T3 environment;
|
|
- the parent T3 thread;
|
|
- the concrete provider instance; and
|
|
- the provider session.
|
|
|
|
The credential grants `preview` and `orchestration` capabilities. Credentials
|
|
expire after a maximum lifetime, expire when idle, and are revoked when the
|
|
provider session is released. The raw token is not persisted in orchestration
|
|
state.
|
|
|
|
The MCP HTTP server resolves the bearer token and supplies the resulting
|
|
`McpInvocationScope` to tool handlers. Orchestration handlers additionally
|
|
check the `orchestration` capability before reading or mutating state.
|
|
|
|
## Provider Injection
|
|
|
|
### Codex V2
|
|
|
|
Codex app-server receives the remote MCP server through command-line config
|
|
overrides:
|
|
|
|
```text
|
|
-c mcp_servers.t3-code.url=http://127.0.0.1:<port>/mcp
|
|
-c mcp_servers.t3-code.bearer_token_env_var="T3_MCP_BEARER_TOKEN"
|
|
```
|
|
|
|
The provider-session token is placed in `T3_MCP_BEARER_TOKEN`. Both the
|
|
production Codex launcher and the injectable test launcher use the same
|
|
projection helper.
|
|
|
|
### Claude Agent SDK V2
|
|
|
|
Claude receives an HTTP MCP server in its query options:
|
|
|
|
```ts
|
|
{
|
|
mcpServers: {
|
|
"t3-code": {
|
|
type: "http",
|
|
url: "http://127.0.0.1:<port>/mcp",
|
|
headers: {
|
|
Authorization: "Bearer <provider-session-token>",
|
|
},
|
|
},
|
|
},
|
|
allowedTools: [
|
|
// existing allowed tools
|
|
"mcp__t3-code__*",
|
|
],
|
|
}
|
|
```
|
|
|
|
The adapter logs only whether MCP configuration exists; it does not log the
|
|
server headers or token.
|
|
|
|
### Cursor Agent SDK V2
|
|
|
|
Cursor receives the same authenticated HTTP MCP endpoint through the SDK's
|
|
`mcpServers` agent and send options. The adapter passes the authorization header
|
|
to the SDK but projects only redacted option metadata into protocol diagnostics.
|
|
|
|
### Initial Provider Support
|
|
|
|
The V2 provider adapters are Codex, Claude Agent SDK, and Cursor Agent SDK.
|
|
Capability discovery still reports other registered provider instances, but marks them
|
|
unavailable for orchestration when no V2 adapter exists. This keeps provider
|
|
selection model-visible without allowing a request that cannot run.
|
|
|
|
## Tool Surface
|
|
|
|
The server exposes five orchestration tools.
|
|
|
|
### `orchestrator_capabilities`
|
|
|
|
Returns:
|
|
|
|
- the inherited provider instance and model;
|
|
- the parent runtime and interaction modes;
|
|
- registered provider instances and advertised models;
|
|
- whether each provider can run a child task; and
|
|
- feature flags for polling, cancellation, and batch thread creation.
|
|
|
|
Unavailable providers include model-visible constraints such as missing V2
|
|
adapter support, disabled state, missing executable, or missing authentication.
|
|
|
|
### `delegate_task`
|
|
|
|
Creates a T3-owned child thread and immediately dispatches the supplied task
|
|
prompt.
|
|
|
|
```ts
|
|
type DelegateTaskInput = {
|
|
task: string;
|
|
target?: {
|
|
providerInstanceId?: string;
|
|
driverKind?: string;
|
|
model?: string;
|
|
};
|
|
title?: string;
|
|
role?: "implementation" | "research" | "review" | "design" | "test" | "general";
|
|
mode?: "async" | "wait";
|
|
timeoutMs?: number;
|
|
clientRequestId?: string;
|
|
runtimeMode?: "inherit" | "approval-required" | "auto-accept-edits" | "full-access";
|
|
interactionMode?: "inherit" | "plan" | "default";
|
|
};
|
|
```
|
|
|
|
Provider, model, runtime mode, and interaction mode inherit from the parent
|
|
when omitted. Selecting a different provider without a model uses that
|
|
provider's first advertised model.
|
|
|
|
Delegation requires an active parent run owned by the MCP credential's
|
|
provider session. The request becomes the V2 command
|
|
`delegated_task.request`.
|
|
|
|
`mode: "async"` returns the current durable state immediately.
|
|
`mode: "wait"` polls the same durable state until it becomes terminal or the
|
|
timeout expires. A wait timeout does not cancel the child; the result sets
|
|
`waitTimedOut: true`, and the caller can continue with `task_status`.
|
|
|
|
```ts
|
|
type DelegateTaskResult = {
|
|
taskId: string;
|
|
childThreadId: string;
|
|
childRunId: string | null;
|
|
childNodeId: string;
|
|
status: "queued" | "running" | "waiting" | "completed" | "failed" | "cancelled" | "interrupted";
|
|
providerInstanceId: string;
|
|
model: string | null;
|
|
summary: string | null;
|
|
resultContextTransferId: string | null;
|
|
waitTimedOut: boolean;
|
|
};
|
|
```
|
|
|
|
### `task_status`
|
|
|
|
Reads a delegated task from the parent thread's durable projection. A task ID
|
|
from another parent thread is rejected. Terminal results include the child
|
|
summary and the durable `subagent_result` context transfer ID when available.
|
|
|
|
### `task_cancel`
|
|
|
|
Interrupts the active child run through the normal V2 `run.interrupt` command.
|
|
It is idempotent for terminal tasks and accepts an optional cancellation
|
|
reason.
|
|
|
|
### `create_threads`
|
|
|
|
Creates between one and twenty ordinary top-level T3 threads:
|
|
|
|
```ts
|
|
type CreateThreadsInput = {
|
|
threads: Array<{
|
|
prompt?: string;
|
|
title?: string;
|
|
target?: {
|
|
providerInstanceId?: string;
|
|
driverKind?: string;
|
|
model?: string;
|
|
};
|
|
runtimeMode?: "inherit" | "approval-required" | "auto-accept-edits" | "full-access";
|
|
interactionMode?: "inherit" | "plan" | "default";
|
|
}>;
|
|
clientRequestId?: string;
|
|
};
|
|
```
|
|
|
|
Each entry independently resolves provider, model, and modes. The new threads
|
|
inherit the parent's project, branch, and worktree path, but they have no
|
|
sub-agent lineage. Entries with a prompt immediately dispatch a run; entries
|
|
without a prompt remain idle.
|
|
|
|
## Delegated Task Lifecycle
|
|
|
|
The MCP server is a command ingress into V2. It does not call provider adapters
|
|
directly.
|
|
|
|
```text
|
|
provider model
|
|
-> MCP tools/call delegate_task
|
|
-> authenticated OrchestratorMcpService
|
|
-> V2 delegated_task.request command
|
|
-> child thread + child run
|
|
-> parent app_owned subagent projection
|
|
-> parent/child execution nodes
|
|
-> consumed subagent_spawn context transfer
|
|
-> normal provider effect and runtime ingestion
|
|
-> child run reaches a terminal state
|
|
-> parent subagent/node/turn item finalized
|
|
-> consumed subagent_result context transfer
|
|
-> wait result or later task_status result
|
|
```
|
|
|
|
The child thread has lineage relationship `subagent` and points back to the
|
|
parent node. The parent gets an `app_owned` sub-agent projection and a
|
|
sub-agent turn item so the existing debug UI can render progress.
|
|
|
|
Terminal provider events trigger finalization. The event stream first replays
|
|
persisted events and then follows live events, so finalization also runs after
|
|
a server restart. An existing `subagent_result` transfer makes finalization
|
|
idempotent.
|
|
|
|
The result summary prefers the latest assistant content from the child run and
|
|
falls back to a terminal-status message when no assistant text exists.
|
|
|
|
## Policy And Idempotency
|
|
|
|
- A child runtime mode may stay equal to or become narrower than the parent
|
|
mode. It may not escalate privileges.
|
|
- A child interaction mode may stay equal to or narrow from `default` to
|
|
`plan`. It may not escalate from `plan` to `default`.
|
|
- Provider instances must be enabled, installed, available, authenticated, and
|
|
backed by a V2 adapter.
|
|
- A requested model must be advertised by the selected provider when the
|
|
provider publishes a model list.
|
|
- `clientRequestId` derives stable command, thread, and message IDs within the
|
|
provider session. Retrying the same call returns the same durable work.
|
|
- Calls without `clientRequestId` receive a generated request key and create
|
|
new work.
|
|
|
|
Expected denials use the typed `OrchestratorMcpFailure` result:
|
|
|
|
```text
|
|
capability_denied
|
|
parent_not_active
|
|
provider_unavailable
|
|
model_unavailable
|
|
runtime_mode_escalation_denied
|
|
interaction_mode_escalation_denied
|
|
task_not_found
|
|
task_not_cancellable
|
|
invalid_request
|
|
orchestration_error
|
|
```
|
|
|
|
## Code Ownership
|
|
|
|
- Shared schemas: `packages/contracts/src/orchestratorMcp.ts`
|
|
- MCP service: `apps/server/src/mcp/OrchestratorMcpService.ts`
|
|
- Tool definitions and handlers:
|
|
`apps/server/src/mcp/toolkits/orchestrator/`
|
|
- HTTP registration and authentication:
|
|
`apps/server/src/mcp/McpHttpServer.ts`
|
|
- Credential lifecycle: `apps/server/src/mcp/McpSessionRegistry.ts`
|
|
- Provider injection:
|
|
`apps/server/src/orchestration-v2/ProviderSessionManager.ts` and V2 adapters
|
|
- Durable delegated-task command and finalization:
|
|
`apps/server/src/orchestration-v2/Orchestrator.ts`
|
|
|
|
## Verification
|
|
|
|
The integration test uses the real MCP toolkit registration, V2 orchestrator,
|
|
SQL persistence, event ingestion, projections, and checkpoints. Only the
|
|
external provider adapters are deterministic test implementations.
|
|
|
|
Coverage includes:
|
|
|
|
- capability discovery;
|
|
- cross-provider delegated completion;
|
|
- prompt-only child context;
|
|
- parent and child lineage projections;
|
|
- spawn and result context transfers;
|
|
- async status polling;
|
|
- cancellation;
|
|
- batch ordinary-thread creation;
|
|
- inheritance and per-thread provider overrides; and
|
|
- idempotent retries.
|
|
|
|
Provider adapter tests separately verify Codex, Claude, and Cursor MCP injection, and
|
|
the provider-session manager test verifies that credentials exist before an
|
|
adapter opens and are revoked when it closes.
|