@zvada/agent-server 0.3.21 → 0.3.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -0
- package/README.md +2 -1
- package/dist/types/client/connection.d.ts +2 -0
- package/dist/types/client/connection.d.ts.map +1 -1
- package/dist/types/client/logins.d.ts +2 -2
- package/dist/types/client/logins.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/client.d.ts +9 -0
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +3 -1
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +7 -5
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +48 -8
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/options.d.ts +6 -2
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +10 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
- package/dist/types/core/agents/plan.d.ts +2 -13
- package/dist/types/core/agents/plan.d.ts.map +1 -1
- package/dist/types/core/index.d.ts +1 -1
- package/dist/types/core/index.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +2 -1
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +2 -0
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/core/utils/errors.d.ts +8 -0
- package/dist/types/core/utils/errors.d.ts.map +1 -1
- package/dist/types/operator/index.d.ts +11 -0
- package/dist/types/operator/index.d.ts.map +1 -0
- package/dist/types/operator/mcp.d.ts +33 -0
- package/dist/types/operator/mcp.d.ts.map +1 -0
- package/dist/types/operator/operator.d.ts +206 -0
- package/dist/types/operator/operator.d.ts.map +1 -0
- package/dist/types/operator/render.d.ts +87 -0
- package/dist/types/operator/render.d.ts.map +1 -0
- package/dist/types/operator/view.d.ts +44 -0
- package/dist/types/operator/view.d.ts.map +1 -0
- package/dist/types/protocol/plan.d.ts +20 -0
- package/dist/types/protocol/plan.d.ts.map +1 -0
- package/dist/types/server/acp/binding.d.ts.map +1 -1
- package/dist/types/sync/worker.d.ts.map +1 -1
- package/docs/consuming.md +188 -14
- package/docs/harnesses.md +13 -8
- package/package.json +6 -1
- package/src/client/connection.ts +10 -0
- package/src/client/logins.ts +23 -14
- package/src/core/agents/codex-app-server/client.ts +32 -2
- package/src/core/agents/codex-app-server/codex-agent.ts +26 -5
- package/src/core/agents/codex-app-server/homes.ts +16 -9
- package/src/core/agents/codex-app-server/mcp.ts +122 -21
- package/src/core/agents/codex-app-server/options.ts +10 -1
- package/src/core/agents/codex-app-server/session.ts +46 -9
- package/src/core/agents/codex-app-server/turn.ts +26 -12
- package/src/core/agents/plan.ts +3 -19
- package/src/core/index.ts +1 -0
- package/src/core/runtime/agent-runtime.ts +22 -16
- package/src/core/runtime/turn.ts +2 -0
- package/src/core/utils/errors.ts +13 -0
- package/src/operator/index.ts +40 -0
- package/src/operator/mcp.ts +171 -0
- package/src/operator/operator.ts +778 -0
- package/src/operator/render.ts +435 -0
- package/src/operator/view.ts +82 -0
- package/src/protocol/plan.ts +35 -0
- package/src/server/acp/binding.ts +3 -2
- package/src/sync/worker.ts +4 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operator tools over MCP, with no dependency: one function answers one
|
|
3
|
+
* JSON-RPC message (or a batch) of the Model Context Protocol's tool surface.
|
|
4
|
+
*
|
|
5
|
+
* It serves stateless Streamable HTTP. A product's route parses the POST
|
|
6
|
+
* body, calls the handler with the caller it authenticated, and answers
|
|
7
|
+
* `200 application/json` with the result, or `202` with no body when the
|
|
8
|
+
* result is null (a notification). It answers GET with `405`: the server
|
|
9
|
+
* never opens a stream of its own.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// @ref LLP 0014#the-mcp-binding — the engine answers MCP's tool methods; the route, its authentication and its caller are the host's
|
|
13
|
+
|
|
14
|
+
import { OperatorError, type ToolInfo } from "./operator.ts";
|
|
15
|
+
import { quoted } from "./render.ts";
|
|
16
|
+
|
|
17
|
+
/** Anything that lists and runs tools: an operator, or a host's wrapper around one. */
|
|
18
|
+
export interface ToolSource<Caller> {
|
|
19
|
+
tools(caller: Caller): ToolInfo[];
|
|
20
|
+
call(name: string, args: unknown, caller: Caller): Promise<string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface McpServerInfo {
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
/** Words for the client's model about how to use the server. */
|
|
28
|
+
instructions?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The MCP protocol versions this binding answers, newest first: the list the
|
|
33
|
+
* official SDK (`@modelcontextprotocol/sdk` 1.32, `SUPPORTED_PROTOCOL_VERSIONS`
|
|
34
|
+
* in its `types.js`) accepts. The tool methods used here are the same in each.
|
|
35
|
+
*/
|
|
36
|
+
export const MCP_PROTOCOL_VERSIONS = [
|
|
37
|
+
"2025-11-25",
|
|
38
|
+
"2025-06-18",
|
|
39
|
+
"2025-03-26",
|
|
40
|
+
"2024-11-05",
|
|
41
|
+
"2024-10-07",
|
|
42
|
+
] as const;
|
|
43
|
+
|
|
44
|
+
/** The most messages one batch may hold. */
|
|
45
|
+
const MAX_BATCH = 20;
|
|
46
|
+
|
|
47
|
+
export type McpHandler<Caller> = (message: unknown, caller: Caller) => Promise<unknown | null>;
|
|
48
|
+
|
|
49
|
+
type Id = string | number | null;
|
|
50
|
+
const ok = (id: Id, result: unknown) => ({ jsonrpc: "2.0" as const, id, result });
|
|
51
|
+
const fail = (id: Id, code: number, message: string) => ({
|
|
52
|
+
jsonrpc: "2.0" as const,
|
|
53
|
+
id,
|
|
54
|
+
error: { code, message },
|
|
55
|
+
});
|
|
56
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
57
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
58
|
+
const text = (value: string, isError?: boolean) => ({
|
|
59
|
+
content: [{ type: "text", text: value }],
|
|
60
|
+
...(isError ? { isError: true } : {}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/** Answer one MCP JSON-RPC message (or batch array). Returns the response, or null for a notification. */
|
|
64
|
+
export function createMcpHandler<Caller>(
|
|
65
|
+
source: ToolSource<Caller>,
|
|
66
|
+
info: McpServerInfo,
|
|
67
|
+
): McpHandler<Caller> {
|
|
68
|
+
const serverInfo = {
|
|
69
|
+
name: info.name,
|
|
70
|
+
version: info.version,
|
|
71
|
+
...(info.title ? { title: info.title } : {}),
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
async function one(message: unknown, caller: Caller): Promise<unknown | null> {
|
|
75
|
+
if (!isRecord(message) || message.jsonrpc !== "2.0") {
|
|
76
|
+
return fail(isRecord(message) ? idOf(message) : null, -32600, "Invalid Request");
|
|
77
|
+
}
|
|
78
|
+
// A response to a request this server never sent, or a notification: nothing to answer.
|
|
79
|
+
if (typeof message.method !== "string") {
|
|
80
|
+
return "id" in message && ("result" in message || "error" in message)
|
|
81
|
+
? null
|
|
82
|
+
: fail(idOf(message), -32600, "Invalid Request");
|
|
83
|
+
}
|
|
84
|
+
if (!("id" in message)) return null;
|
|
85
|
+
const id = idOf(message);
|
|
86
|
+
const params = isRecord(message.params) ? message.params : {};
|
|
87
|
+
switch (message.method) {
|
|
88
|
+
case "initialize": {
|
|
89
|
+
const asked = params.protocolVersion;
|
|
90
|
+
const protocolVersion =
|
|
91
|
+
MCP_PROTOCOL_VERSIONS.find((v) => v === asked) ?? MCP_PROTOCOL_VERSIONS[0];
|
|
92
|
+
return ok(id, {
|
|
93
|
+
protocolVersion,
|
|
94
|
+
capabilities: { tools: { listChanged: false } },
|
|
95
|
+
serverInfo,
|
|
96
|
+
...(info.instructions ? { instructions: info.instructions } : {}),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
case "ping":
|
|
100
|
+
return ok(id, {});
|
|
101
|
+
case "tools/list": {
|
|
102
|
+
try {
|
|
103
|
+
const tools = source.tools(caller);
|
|
104
|
+
return ok(id, {
|
|
105
|
+
tools: tools.map((tool) => ({
|
|
106
|
+
name: tool.name,
|
|
107
|
+
title: tool.title,
|
|
108
|
+
description: tool.description,
|
|
109
|
+
inputSchema: tool.inputSchema,
|
|
110
|
+
annotations: {
|
|
111
|
+
title: tool.title,
|
|
112
|
+
readOnlyHint: tool.readOnly,
|
|
113
|
+
destructiveHint: tool.destructive === true,
|
|
114
|
+
openWorldHint: tool.openWorld === true,
|
|
115
|
+
},
|
|
116
|
+
})),
|
|
117
|
+
});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return fail(id, -32603, messageOf(error));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
case "tools/call": {
|
|
123
|
+
if (typeof params.name !== "string" || !params.name) {
|
|
124
|
+
return fail(id, -32602, "tools/call needs the tool's name");
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
return ok(id, text(await source.call(params.name, params.arguments ?? {}, caller)));
|
|
128
|
+
} catch (error) {
|
|
129
|
+
// The model reads the reason and can act on it; a JSON-RPC error it would not see.
|
|
130
|
+
// A refusal's words are meant for it; any other failure's may be an agent's or a
|
|
131
|
+
// repository's, so it is one quoted line, never a line that reads as a hint.
|
|
132
|
+
return ok(
|
|
133
|
+
id,
|
|
134
|
+
text(
|
|
135
|
+
error instanceof OperatorError
|
|
136
|
+
? error.message
|
|
137
|
+
: `The tool failed: ${quoted(messageOf(error))}`,
|
|
138
|
+
true,
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
default:
|
|
144
|
+
return fail(id, -32601, `Method not found: ${message.method}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return async (message, caller) => {
|
|
149
|
+
if (!Array.isArray(message)) return one(message, caller);
|
|
150
|
+
if (!message.length) return fail(null, -32600, "Invalid Request: empty batch");
|
|
151
|
+
if (message.length > MAX_BATCH) {
|
|
152
|
+
return fail(null, -32600, `Invalid Request: a batch holds at most ${MAX_BATCH} messages`);
|
|
153
|
+
}
|
|
154
|
+
// One after another: a batch is never more calls at once than one message.
|
|
155
|
+
const replies: unknown[] = [];
|
|
156
|
+
for (const each of message) {
|
|
157
|
+
const answer = await one(each, caller);
|
|
158
|
+
if (answer !== null) replies.push(answer);
|
|
159
|
+
}
|
|
160
|
+
return replies.length ? replies : null;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function idOf(message: Record<string, unknown>): Id {
|
|
165
|
+
const id = message.id;
|
|
166
|
+
return typeof id === "string" || typeof id === "number" ? id : null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function messageOf(error: unknown): string {
|
|
170
|
+
return error instanceof Error ? error.message : String(error);
|
|
171
|
+
}
|