Official
claude
Claude Code agent provider: runs the Claude Code CLI headless for each account.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/claude@0.2.7
Permissions in 0.2.7
Files
mcp.ts8.8 KB
// MCP Apps use Claude's own connections and permission checks.
import * as Effect from "effect/Effect";
import * as z from "zod";
import { Storage, parse } from "divergence/effect";
import type { McpCallToolParams, McpReadResourceParams } from "divergence/protocol";
import type { Session } from "./session.ts";
import type { ToolContent, ToolCall, TranscriptItem, Emission } from "./types.ts";
import { ProviderError } from "./errors.ts";
import { errorMessage } from "../sdk/errors.ts";
import * as wire from "./wire.ts";
const REQUIRED = ["mcp_tool_ui_meta_v1", "mcp_read_resource_v1", "mcp_call_result_not_for_model_v1", "mcp_call_check_permissions_v1"];
export const supportsApps = (capabilities: ReadonlySet<string> | null) => REQUIRED.every((flag) => capabilities?.has(flag));
export const callResults = new WeakMap<ToolCall, unknown>();
const bindingSchema = z.object({ server: z.string(), tool: z.string(), resourceUri: z.string() });
const serversSchema = z.object({ mcpServers: z.array(z.object({
name: z.string(),
tools: z.array(z.object({ name: z.string(), _meta: wire.object.optional() })).optional(),
})), bindings: z.record(z.string(), bindingSchema).optional() });
type Servers = z.infer<typeof serversSchema>["mcpServers"];
const normalized = (name: string) => {
const clean = name.replace(/[^a-zA-Z0-9_-]/g, "_");
return name.startsWith("claude.ai ") ? clean.replace(/_+/g, "_").replace(/^_|_$/g, "") : clean;
};
const fullName = (server: string, tool: string) => `mcp__${normalized(server)}__${normalized(tool)}`;
const capabilitySchema = z.object({ version: z.string(), capabilities: z.array(z.string()) });
/// A resumed process can reuse advertised flags from the same CLI build.
export const capabilitiesFor = Effect.fn("Claude.mcpCapabilities")(function* (session: Session, version: string | null | undefined, partial = true) {
const storage = yield* Storage;
const key = `mcp-capabilities:${session.ctx.agentId}`;
if (supportsApps(session.capabilities)) {
if (version) yield* storage.set(key, { version, capabilities: [...session.capabilities!] });
} else if (version && partial) {
const known = yield* storage.get(key, capabilitySchema);
if (known?.version === version)
session.capabilities = new Set([...(session.capabilities ?? []), ...known.capabilities.filter((flag) =>
flag === "mcp_tool_ui_meta_v1" || flag === "mcp_read_resource_v1",
)]);
}
});
/// Claude writes content of one text block as that text alone.
const blocks = (content: unknown) => typeof content === "string" ? [{ type: "text", text: content }] : content;
/// Only fields Claude actually exposes become the app's result: history
/// keeps the content (or its text) with the rest in `mcpMeta`, and the
/// stream sends an object with that content.
export function mcpResult(raw: unknown, meta?: unknown): unknown {
if (typeof raw === "string" || Array.isArray(raw)) return { content: blocks(raw), ...wire.objectOf(meta) };
const result = wire.objectOf(raw);
const content = blocks(result.content);
if (!Array.isArray(content)) return undefined;
return Object.fromEntries(["content", "structuredContent", "_meta", "isError"].flatMap((key) =>
Object.hasOwn(result, key) ? [[key, key === "content" ? content : result[key]]] : [],
));
}
export class McpApps {
enabled: boolean;
servers: Servers = [];
calling = 0;
bindings: Record<string, z.infer<typeof bindingSchema>> = {};
dirty = false;
/// A call named a tool the last status did not list: its server finished
/// connecting after that read, so the session reads the status again.
stale = false;
unlisted = new Set<string>();
calls = new Map<string, { name: string; content: ToolContent[]; result?: unknown; task: string | null }>();
constructor(enabled = false) {
this.enabled = enabled;
}
tool(name: string) {
for (const server of this.servers)
for (const tool of server.tools ?? [])
if (fullName(server.name, tool.name) === name) return { server: server.name, tool: tool.name, meta: tool._meta };
return undefined;
}
content(name: string, fallback: ToolContent[], result?: unknown, id?: string): ToolContent[] {
if (!this.enabled) return fallback;
const bound = id ? this.bindings[id] : undefined;
if (bound) return [{ type: "mcp_app", ...bound, ...(result !== undefined ? { result } : {}) }, ...fallback];
const definition = this.tool(name);
const ui = wire.objectOf(definition?.meta?.ui);
const uri = ui.resourceUri ?? definition?.meta?.["ui/resourceUri"];
if (!definition || typeof uri !== "string" || !uri.startsWith("ui://")) return fallback;
const binding = { server: definition.server, tool: definition.tool, resourceUri: uri };
if (id) { this.bindings[id] = binding; this.dirty = true; }
return [{ type: "mcp_app", ...binding,
...(result !== undefined ? { result } : {}) }, ...fallback];
}
track(id: string, name: string, content: ToolContent[], task: string | null, result?: unknown) {
if (!this.enabled || !name.startsWith("mcp__")) return content;
this.calls.set(id, { name, content, task, result });
if (!this.bindings[id] && !this.tool(name) && !this.unlisted.has(name)) {
this.unlisted.add(name);
this.stale = true;
}
return this.content(name, content, result, id);
}
replace(servers: Servers): Emission[] {
this.servers = servers;
return [...this.calls].flatMap(([id, call]) => {
const content = this.content(call.name, call.content, call.result, id);
return content === call.content ? [] : [{ task: call.task, kind: { event: "tool_call_updated" as const, id, content } }];
});
}
decorateHistory(items: TranscriptItem[]) {
if (!this.enabled) return;
for (const item of items) {
if (item.role === "task") this.decorateHistory(item.items ?? []);
if (item.role !== "tool") continue;
const fallback = item.call.content?.filter((block) => block.type !== "mcp_app") ?? [];
const content = this.content(item.call.name, fallback, callResults.get(item.call), item.call.id);
if (content.length) item.call.content = content;
}
}
}
export const loadApps = Effect.fn("Claude.loadApps")(function* (sessionId: string, enabled: boolean) {
const apps = new McpApps(enabled);
if (enabled) {
const stored = yield* (yield* Storage).get(`mcp:${sessionId}`, serversSchema);
if (stored) { apps.servers = stored.mcpServers; apps.bindings = stored.bindings ?? {}; }
}
return apps;
});
export const refreshApps = Effect.fn("Claude.refreshApps")(function* (session: Session) {
if (!session.mapper.mcp.enabled || !supportsApps(session.capabilities)) return;
const response = yield* parse("Claude MCP tools", serversSchema, yield* session.controlRequest({ subtype: "mcp_status" }));
for (const event of session.mapper.mcp.replace(response.mcpServers)) session.emitMapped(event);
yield* saveApps(session);
});
/// Reading the status must not hold up the stream it was asked from.
export const refreshAppsLater = (session: Session) => refreshApps(session).pipe(
Effect.catchCause((cause) => Effect.sync(() => session.emitEvent({
event: "notice", level: "warning", message: `Claude MCP tools could not be read: ${errorMessage(cause)}`,
}))),
Effect.forkIn(session.ctx.scope),
);
export const saveApps =Effect.fn("Claude.saveApps")(function* (session: Session) {
const apps = session.mapper.mcp;
apps.dirty = false;
yield* (yield* Storage).set(`mcp:${session.id}`, { mcpServers: apps.servers, bindings: apps.bindings });
});
const ready = Effect.fn("Claude.appsReady")(function* (session: Session) {
yield* session.start();
if (!session.mapper.mcp.enabled || !supportsApps(session.capabilities))
return yield* new ProviderError({ message: "This Claude process has not advertised MCP Apps support." });
});
export const readResource = Effect.fn("Claude.readResource")(function* (session: Session, params: McpReadResourceParams) {
yield* ready(session);
return yield* session.controlRequest({ subtype: "mcp_read_resource", serverName: params.server, uri: params.uri });
});
export const callTool = Effect.fn("Claude.callTool")(function* (session: Session, params: McpCallToolParams) {
yield* ready(session);
yield* refreshApps(session);
const tool = session.mapper.mcp.servers.find((server) => server.name === params.server)?.tools?.find((tool) => tool.name === params.name);
const visibility = wire.objectOf(tool?._meta?.ui).visibility;
if (Array.isArray(visibility) && !visibility.includes("app"))
return yield* new ProviderError({ message: `The MCP tool ${params.name} is not visible to apps.` });
session.mapper.mcp.calling++;
return yield* session.controlRequest({
subtype: "mcp_call", tool: fullName(params.server, params.name), arguments: params.arguments,
result_not_for_model: true, check_permissions: true,
}, 30 * 60_000).pipe(Effect.ensuring(Effect.sync(() => { session.mapper.mcp.calling--; })));
});Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.7latest | Nov 21, 2026 | >=5 <6 | 207.5 KB | 4 permissions | Listed |
| 0.2.6 | Nov 16, 2026 | >=5 <6 | 206 KB | 4 permissions | Listed |
| 0.2.5 | Nov 11, 2026 | >=5 <6 | 168.3 KB | 4 permissions | Listed |
| 0.2.4 | Nov 9, 2026 | >=5 <6 | 166.1 KB | 4 permissions | Listed |
| 0.2.3 | Oct 27, 2026 | >=2 <4 | 151.6 KB | 4 permissions | Listed |
No comments yet.