@zauso-ai/capstan-agent 1.0.0-beta.5 → 1.0.0-beta.7
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/dist/a2a.d.ts.map +1 -1
- package/dist/a2a.js +42 -21
- package/dist/a2a.js.map +1 -1
- package/dist/commerce.d.ts +62 -0
- package/dist/commerce.d.ts.map +1 -0
- package/dist/commerce.js +39 -0
- package/dist/commerce.js.map +1 -0
- package/dist/index.d.ts +12 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/langchain.d.ts +52 -0
- package/dist/langchain.d.ts.map +1 -0
- package/dist/langchain.js +157 -0
- package/dist/langchain.js.map +1 -0
- package/dist/llm.d.ts +41 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +166 -0
- package/dist/llm.js.map +1 -0
- package/dist/mcp-client.d.ts +45 -0
- package/dist/mcp-client.d.ts.map +1 -0
- package/dist/mcp-client.js +289 -0
- package/dist/mcp-client.js.map +1 -0
- package/dist/mcp.d.ts +23 -0
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +85 -1
- package/dist/mcp.js.map +1 -1
- package/dist/registry.d.ts +21 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +48 -1
- package/dist/registry.js.map +1 -1
- package/dist/telemetry.d.ts +8 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +48 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/testing.d.ts +84 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +490 -0
- package/dist/testing.js.map +1 -0
- package/package.json +9 -2
package/dist/llm.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// LLM provider adapter interface and built-in implementations
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// OpenAI-compatible provider
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
export function openaiProvider(config) {
|
|
8
|
+
const baseUrl = config.baseUrl ?? "https://api.openai.com/v1";
|
|
9
|
+
const defaultModel = config.model ?? "gpt-4o";
|
|
10
|
+
return {
|
|
11
|
+
name: "openai",
|
|
12
|
+
async chat(messages, options) {
|
|
13
|
+
const body = {
|
|
14
|
+
model: options?.model ?? defaultModel,
|
|
15
|
+
messages: options?.systemPrompt
|
|
16
|
+
? [{ role: "system", content: options.systemPrompt }, ...messages]
|
|
17
|
+
: messages,
|
|
18
|
+
};
|
|
19
|
+
if (options?.temperature !== undefined)
|
|
20
|
+
body["temperature"] = options.temperature;
|
|
21
|
+
if (options?.maxTokens !== undefined)
|
|
22
|
+
body["max_tokens"] = options.maxTokens;
|
|
23
|
+
if (options?.responseFormat)
|
|
24
|
+
body["response_format"] = {
|
|
25
|
+
type: "json_schema",
|
|
26
|
+
json_schema: {
|
|
27
|
+
schema: options.responseFormat,
|
|
28
|
+
name: "response",
|
|
29
|
+
strict: true,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const res = await fetch(`${baseUrl}/chat/completions`, {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify(body),
|
|
39
|
+
});
|
|
40
|
+
if (!res.ok)
|
|
41
|
+
throw new Error(`LLM error ${res.status}: ${await res.text()}`);
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
const data = (await res.json());
|
|
44
|
+
return {
|
|
45
|
+
content: data.choices?.[0]?.message?.content ?? "",
|
|
46
|
+
model: data.model ?? defaultModel,
|
|
47
|
+
usage: data.usage
|
|
48
|
+
? {
|
|
49
|
+
promptTokens: data.usage.prompt_tokens,
|
|
50
|
+
completionTokens: data.usage.completion_tokens,
|
|
51
|
+
totalTokens: data.usage.total_tokens,
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
finishReason: data.choices?.[0]?.finish_reason,
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
async *stream(messages, options) {
|
|
58
|
+
const body = {
|
|
59
|
+
model: options?.model ?? defaultModel,
|
|
60
|
+
messages: options?.systemPrompt
|
|
61
|
+
? [{ role: "system", content: options.systemPrompt }, ...messages]
|
|
62
|
+
: messages,
|
|
63
|
+
stream: true,
|
|
64
|
+
};
|
|
65
|
+
if (options?.temperature !== undefined)
|
|
66
|
+
body["temperature"] = options.temperature;
|
|
67
|
+
if (options?.maxTokens !== undefined)
|
|
68
|
+
body["max_tokens"] = options.maxTokens;
|
|
69
|
+
const res = await fetch(`${baseUrl}/chat/completions`, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: {
|
|
72
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
73
|
+
"Content-Type": "application/json",
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify(body),
|
|
76
|
+
});
|
|
77
|
+
if (!res.ok)
|
|
78
|
+
throw new Error(`LLM error ${res.status}`);
|
|
79
|
+
if (!res.body)
|
|
80
|
+
throw new Error("No body");
|
|
81
|
+
const reader = res.body.getReader();
|
|
82
|
+
const decoder = new TextDecoder();
|
|
83
|
+
let buf = "";
|
|
84
|
+
while (true) {
|
|
85
|
+
const { done, value } = await reader.read();
|
|
86
|
+
if (done)
|
|
87
|
+
break;
|
|
88
|
+
buf += decoder.decode(value, { stream: true });
|
|
89
|
+
const lines = buf.split("\n");
|
|
90
|
+
buf = lines.pop() ?? "";
|
|
91
|
+
for (const line of lines) {
|
|
92
|
+
if (!line.startsWith("data: "))
|
|
93
|
+
continue;
|
|
94
|
+
const d = line.slice(6).trim();
|
|
95
|
+
if (d === "[DONE]") {
|
|
96
|
+
yield { content: "", done: true };
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const p = JSON.parse(d);
|
|
101
|
+
const c = p.choices?.[0]?.delta?.content ?? "";
|
|
102
|
+
if (c)
|
|
103
|
+
yield { content: c, done: false };
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// skip malformed chunks
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
yield { content: "", done: true };
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Anthropic provider
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
export function anthropicProvider(config) {
|
|
118
|
+
const baseUrl = config.baseUrl ?? "https://api.anthropic.com/v1";
|
|
119
|
+
const defaultModel = config.model ?? "claude-sonnet-4-20250514";
|
|
120
|
+
return {
|
|
121
|
+
name: "anthropic",
|
|
122
|
+
async chat(messages, options) {
|
|
123
|
+
const sys = options?.systemPrompt ??
|
|
124
|
+
messages.find((m) => m.role === "system")?.content;
|
|
125
|
+
const msgs = messages.filter((m) => m.role !== "system");
|
|
126
|
+
const body = {
|
|
127
|
+
model: options?.model ?? defaultModel,
|
|
128
|
+
messages: msgs,
|
|
129
|
+
max_tokens: options?.maxTokens ?? 4096,
|
|
130
|
+
};
|
|
131
|
+
if (sys)
|
|
132
|
+
body["system"] = sys;
|
|
133
|
+
if (options?.temperature !== undefined)
|
|
134
|
+
body["temperature"] = options.temperature;
|
|
135
|
+
const res = await fetch(`${baseUrl}/messages`, {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers: {
|
|
138
|
+
"x-api-key": config.apiKey,
|
|
139
|
+
"anthropic-version": "2023-06-01",
|
|
140
|
+
"Content-Type": "application/json",
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify(body),
|
|
143
|
+
});
|
|
144
|
+
if (!res.ok)
|
|
145
|
+
throw new Error(`Anthropic error ${res.status}: ${await res.text()}`);
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
147
|
+
const data = (await res.json());
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
149
|
+
const text = data.content?.find((b) => b.type === "text")?.text ?? "";
|
|
150
|
+
return {
|
|
151
|
+
content: text,
|
|
152
|
+
model: data.model ?? defaultModel,
|
|
153
|
+
usage: data.usage
|
|
154
|
+
? {
|
|
155
|
+
promptTokens: data.usage.input_tokens,
|
|
156
|
+
completionTokens: data.usage.output_tokens,
|
|
157
|
+
totalTokens: (data.usage.input_tokens ?? 0) +
|
|
158
|
+
(data.usage.output_tokens ?? 0),
|
|
159
|
+
}
|
|
160
|
+
: undefined,
|
|
161
|
+
finishReason: data.stop_reason,
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8DAA8D;AAC9D,8EAA8E;AAwC9E,8EAA8E;AAC9E,6BAA6B;AAC7B,8EAA8E;AAE9E,MAAM,UAAU,cAAc,CAAC,MAI9B;IACC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,2BAA2B,CAAC;IAC9D,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,IAAI,QAAQ,CAAC;IAE9C,OAAO;QACL,IAAI,EAAE,QAAQ;QAEd,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO;YAC1B,MAAM,IAAI,GAA4B;gBACpC,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,YAAY;gBACrC,QAAQ,EAAE,OAAO,EAAE,YAAY;oBAC7B,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,EAAE,EAAE,GAAG,QAAQ,CAAC;oBAClE,CAAC,CAAC,QAAQ;aACb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ACnC;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;aAC3B,CAAC,CAAC;YAEH,IAAI,CAAC,GAAG,CAAC,EAAE;gBACT,MAAM,IAAI,KAAK,CACb,mBAAmB,GAAG,CAAC,MAAM,KAAK,MAAM,GAAG,CAAC,IAAI,EAAE,EAAE,CACrD,CAAC;YAEJ,8DAA8D;YAC9D,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAQ,CAAC;YACvC,8DAA8D;YAC9D,MAAM,IAAI,GACR,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,EAAE,IAAI,IAAI,EAAE,CAAC;YAChE,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,YAAY;gBACjC,KAAK,EAAE,IAAI,CAAC,KAAK;oBACf,CAAC,CAAC;wBACE,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY;wBACrC,gBAAgB,EAAE,IAAI,CAAC,KAAK,CAAC,aAAa;wBAC1C,WAAW,EACT,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,CAAC;4BAC9B,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,CAAC;qBAClC;oBACH,CAAC,CAAC,SAAS;gBACb,YAAY,EAAE,IAAI,CAAC,WAAW;aAC/B,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client — connect to external MCP servers and consume their tools.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official `@modelcontextprotocol/sdk` Client and
|
|
5
|
+
* StreamableHTTPClientTransport for spec-compliant communication.
|
|
6
|
+
* Falls back to a raw JSON-RPC 2.0 implementation when the SDK
|
|
7
|
+
* transport encounters issues (e.g. the remote server does not
|
|
8
|
+
* support Streamable HTTP).
|
|
9
|
+
*/
|
|
10
|
+
/** Options for connecting to a remote MCP server. */
|
|
11
|
+
export interface McpClientOptions {
|
|
12
|
+
/** MCP server URL (Streamable HTTP endpoint). */
|
|
13
|
+
url: string;
|
|
14
|
+
/** Optional authorization header value (e.g. "Bearer <token>"). */
|
|
15
|
+
authorization?: string;
|
|
16
|
+
/** Client name for identification during the MCP handshake. */
|
|
17
|
+
clientName?: string;
|
|
18
|
+
}
|
|
19
|
+
/** A tool exposed by a remote MCP server. */
|
|
20
|
+
export interface McpTool {
|
|
21
|
+
name: string;
|
|
22
|
+
description?: string | undefined;
|
|
23
|
+
inputSchema?: Record<string, unknown> | undefined;
|
|
24
|
+
}
|
|
25
|
+
/** A connected MCP client that can list and invoke remote tools. */
|
|
26
|
+
export interface McpClient {
|
|
27
|
+
/** List available tools on the remote server. */
|
|
28
|
+
listTools(): Promise<McpTool[]>;
|
|
29
|
+
/** Call a tool by name with optional arguments. */
|
|
30
|
+
callTool(name: string, args?: Record<string, unknown>): Promise<unknown>;
|
|
31
|
+
/** Close the connection. */
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create an MCP client connected to a remote server.
|
|
36
|
+
*
|
|
37
|
+
* Attempts to connect using the official SDK `Client` +
|
|
38
|
+
* `StreamableHTTPClientTransport` first. Falls back to a raw JSON-RPC 2.0
|
|
39
|
+
* implementation if the SDK transport fails (e.g. unsupported server).
|
|
40
|
+
*
|
|
41
|
+
* @param options - Connection options (URL, auth, client name).
|
|
42
|
+
* @returns A connected `McpClient` ready to list and call tools.
|
|
43
|
+
*/
|
|
44
|
+
export declare function createMcpClient(options: McpClientOptions): Promise<McpClient>;
|
|
45
|
+
//# sourceMappingURL=mcp-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-client.d.ts","sourceRoot":"","sources":["../src/mcp-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAUH,qDAAqD;AACrD,MAAM,WAAW,gBAAgB;IAC/B,iDAAiD;IACjD,GAAG,EAAE,MAAM,CAAC;IACZ,mEAAmE;IACnE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,+DAA+D;IAC/D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,6CAA6C;AAC7C,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CACnD;AAED,oEAAoE;AACpE,MAAM,WAAW,SAAS;IACxB,iDAAiD;IACjD,SAAS,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IAChC,mDAAmD;IACnD,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzE,4BAA4B;IAC5B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAyUD;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,SAAS,CAAC,CAuCpB"}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client — connect to external MCP servers and consume their tools.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official `@modelcontextprotocol/sdk` Client and
|
|
5
|
+
* StreamableHTTPClientTransport for spec-compliant communication.
|
|
6
|
+
* Falls back to a raw JSON-RPC 2.0 implementation when the SDK
|
|
7
|
+
* transport encounters issues (e.g. the remote server does not
|
|
8
|
+
* support Streamable HTTP).
|
|
9
|
+
*/
|
|
10
|
+
// SDK imports are dynamic to avoid resolution failures when subpath
|
|
11
|
+
// exports are not declared in the package.json exports map.
|
|
12
|
+
import { withSpan } from "./telemetry.js";
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// SDK-backed implementation
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
class SdkMcpClient {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
client;
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
transport;
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
constructor(client, transport) {
|
|
23
|
+
this.client = client;
|
|
24
|
+
this.transport = transport;
|
|
25
|
+
}
|
|
26
|
+
async listTools() {
|
|
27
|
+
return withSpan("capstan.mcp_client.listTools", {}, async () => {
|
|
28
|
+
const result = await this.client.listTools();
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
return result.tools.map((t) => ({
|
|
31
|
+
name: t.name,
|
|
32
|
+
description: t.description,
|
|
33
|
+
inputSchema: t.inputSchema,
|
|
34
|
+
}));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async callTool(name, args) {
|
|
38
|
+
return withSpan(`capstan.mcp_client.callTool.${name}`, { "capstan.mcp_client.tool": name }, async () => {
|
|
39
|
+
const result = await this.client.callTool({
|
|
40
|
+
name,
|
|
41
|
+
arguments: args,
|
|
42
|
+
});
|
|
43
|
+
// The SDK returns a union type; the standard shape has a `content`
|
|
44
|
+
// array of text/image/audio/resource items.
|
|
45
|
+
if ("content" in result && Array.isArray(result.content)) {
|
|
46
|
+
// If there's a single text content item, unwrap it for convenience.
|
|
47
|
+
if (result.content.length === 1 &&
|
|
48
|
+
result.content[0] != null &&
|
|
49
|
+
"type" in result.content[0] &&
|
|
50
|
+
result.content[0].type === "text") {
|
|
51
|
+
const text = result.content[0].text;
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(text);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return result.content;
|
|
60
|
+
}
|
|
61
|
+
// Compatibility result shape (`toolResult` field).
|
|
62
|
+
if ("toolResult" in result) {
|
|
63
|
+
return result.toolResult;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async close() {
|
|
69
|
+
await this.transport.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Raw JSON-RPC 2.0 fallback implementation
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
class RawMcpClient {
|
|
76
|
+
url;
|
|
77
|
+
headers;
|
|
78
|
+
sessionId;
|
|
79
|
+
nextId = 1;
|
|
80
|
+
constructor(url, headers) {
|
|
81
|
+
this.url = url;
|
|
82
|
+
this.headers = headers;
|
|
83
|
+
}
|
|
84
|
+
/** Perform the MCP initialize handshake. */
|
|
85
|
+
async initialize(clientName) {
|
|
86
|
+
await this.sendRequest("initialize", {
|
|
87
|
+
protocolVersion: "2025-03-26",
|
|
88
|
+
capabilities: {},
|
|
89
|
+
clientInfo: { name: clientName, version: "1.0.0" },
|
|
90
|
+
});
|
|
91
|
+
// Send initialized notification (no response expected).
|
|
92
|
+
await this.sendNotification("notifications/initialized", {});
|
|
93
|
+
}
|
|
94
|
+
async listTools() {
|
|
95
|
+
return withSpan("capstan.mcp_client.listTools", {}, async () => {
|
|
96
|
+
const result = (await this.sendRequest("tools/list", {}));
|
|
97
|
+
return (result.tools ?? []).map((t) => ({
|
|
98
|
+
name: t.name,
|
|
99
|
+
description: t.description,
|
|
100
|
+
inputSchema: t.inputSchema,
|
|
101
|
+
}));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async callTool(name, args) {
|
|
105
|
+
return withSpan(`capstan.mcp_client.callTool.${name}`, { "capstan.mcp_client.tool": name }, async () => {
|
|
106
|
+
const result = (await this.sendRequest("tools/call", {
|
|
107
|
+
name,
|
|
108
|
+
arguments: args ?? {},
|
|
109
|
+
}));
|
|
110
|
+
if (result.content && Array.isArray(result.content)) {
|
|
111
|
+
if (result.content.length === 1 &&
|
|
112
|
+
result.content[0]?.type === "text" &&
|
|
113
|
+
result.content[0].text != null) {
|
|
114
|
+
try {
|
|
115
|
+
return JSON.parse(result.content[0].text);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return result.content[0].text;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return result.content;
|
|
122
|
+
}
|
|
123
|
+
if (result.toolResult !== undefined) {
|
|
124
|
+
return result.toolResult;
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async close() {
|
|
130
|
+
if (this.sessionId) {
|
|
131
|
+
try {
|
|
132
|
+
await fetch(this.url, {
|
|
133
|
+
method: "DELETE",
|
|
134
|
+
headers: {
|
|
135
|
+
...this.headers,
|
|
136
|
+
"Mcp-Session-Id": this.sessionId,
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// Best-effort session termination — ignore errors.
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// -----------------------------------------------------------------------
|
|
146
|
+
// Internal helpers
|
|
147
|
+
// -----------------------------------------------------------------------
|
|
148
|
+
async sendRequest(method, params) {
|
|
149
|
+
const id = this.nextId++;
|
|
150
|
+
const body = {
|
|
151
|
+
jsonrpc: "2.0",
|
|
152
|
+
id,
|
|
153
|
+
method,
|
|
154
|
+
params,
|
|
155
|
+
};
|
|
156
|
+
const reqHeaders = {
|
|
157
|
+
...this.headers,
|
|
158
|
+
"Content-Type": "application/json",
|
|
159
|
+
Accept: "application/json, text/event-stream",
|
|
160
|
+
};
|
|
161
|
+
if (this.sessionId) {
|
|
162
|
+
reqHeaders["Mcp-Session-Id"] = this.sessionId;
|
|
163
|
+
}
|
|
164
|
+
const res = await fetch(this.url, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
headers: reqHeaders,
|
|
167
|
+
body: JSON.stringify(body),
|
|
168
|
+
});
|
|
169
|
+
// Capture session ID from the response.
|
|
170
|
+
const newSessionId = res.headers.get("mcp-session-id");
|
|
171
|
+
if (newSessionId) {
|
|
172
|
+
this.sessionId = newSessionId;
|
|
173
|
+
}
|
|
174
|
+
if (!res.ok) {
|
|
175
|
+
const text = await res.text().catch(() => "");
|
|
176
|
+
throw new Error(`MCP request ${method} failed: HTTP ${String(res.status)} ${text}`);
|
|
177
|
+
}
|
|
178
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
179
|
+
// The server may respond with SSE (text/event-stream) or plain JSON.
|
|
180
|
+
if (contentType.includes("text/event-stream")) {
|
|
181
|
+
return this.parseSseResponse(res, id);
|
|
182
|
+
}
|
|
183
|
+
const json = (await res.json());
|
|
184
|
+
if (json.error) {
|
|
185
|
+
throw new Error(`MCP error ${String(json.error.code)}: ${json.error.message}`);
|
|
186
|
+
}
|
|
187
|
+
return json.result;
|
|
188
|
+
}
|
|
189
|
+
async sendNotification(method, params) {
|
|
190
|
+
const body = {
|
|
191
|
+
jsonrpc: "2.0",
|
|
192
|
+
method,
|
|
193
|
+
params,
|
|
194
|
+
};
|
|
195
|
+
const reqHeaders = {
|
|
196
|
+
...this.headers,
|
|
197
|
+
"Content-Type": "application/json",
|
|
198
|
+
};
|
|
199
|
+
if (this.sessionId) {
|
|
200
|
+
reqHeaders["Mcp-Session-Id"] = this.sessionId;
|
|
201
|
+
}
|
|
202
|
+
const res = await fetch(this.url, {
|
|
203
|
+
method: "POST",
|
|
204
|
+
headers: reqHeaders,
|
|
205
|
+
body: JSON.stringify(body),
|
|
206
|
+
});
|
|
207
|
+
// Capture session ID.
|
|
208
|
+
const newSessionId = res.headers.get("mcp-session-id");
|
|
209
|
+
if (newSessionId) {
|
|
210
|
+
this.sessionId = newSessionId;
|
|
211
|
+
}
|
|
212
|
+
// Notifications may return 202 Accepted or 204 No Content — either is fine.
|
|
213
|
+
if (!res.ok && res.status !== 202 && res.status !== 204) {
|
|
214
|
+
const text = await res.text().catch(() => "");
|
|
215
|
+
throw new Error(`MCP notification ${method} failed: HTTP ${String(res.status)} ${text}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async parseSseResponse(res, expectedId) {
|
|
219
|
+
const text = await res.text();
|
|
220
|
+
// Parse SSE events and find the JSON-RPC response matching our request ID.
|
|
221
|
+
for (const line of text.split("\n")) {
|
|
222
|
+
if (line.startsWith("data: ")) {
|
|
223
|
+
try {
|
|
224
|
+
const parsed = JSON.parse(line.slice(6));
|
|
225
|
+
if (parsed.id === expectedId) {
|
|
226
|
+
if (parsed.error) {
|
|
227
|
+
throw new Error(`MCP error ${String(parsed.error.code)}: ${parsed.error.message}`);
|
|
228
|
+
}
|
|
229
|
+
return parsed.result;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (e) {
|
|
233
|
+
if (e instanceof Error && e.message.startsWith("MCP error")) {
|
|
234
|
+
throw e;
|
|
235
|
+
}
|
|
236
|
+
// Skip unparseable SSE data lines.
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
throw new Error("No matching JSON-RPC response found in SSE stream");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
// Factory function
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
/**
|
|
247
|
+
* Create an MCP client connected to a remote server.
|
|
248
|
+
*
|
|
249
|
+
* Attempts to connect using the official SDK `Client` +
|
|
250
|
+
* `StreamableHTTPClientTransport` first. Falls back to a raw JSON-RPC 2.0
|
|
251
|
+
* implementation if the SDK transport fails (e.g. unsupported server).
|
|
252
|
+
*
|
|
253
|
+
* @param options - Connection options (URL, auth, client name).
|
|
254
|
+
* @returns A connected `McpClient` ready to list and call tools.
|
|
255
|
+
*/
|
|
256
|
+
export async function createMcpClient(options) {
|
|
257
|
+
const clientName = options.clientName ?? "capstan-mcp-client";
|
|
258
|
+
const url = new URL(options.url);
|
|
259
|
+
const requestInit = {};
|
|
260
|
+
if (options.authorization) {
|
|
261
|
+
requestInit.headers = {
|
|
262
|
+
Authorization: options.authorization,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
// --- Attempt 1: SDK transport (lazy import) ---
|
|
266
|
+
try {
|
|
267
|
+
const clientMod = await import("@modelcontextprotocol/sdk/client/index.js");
|
|
268
|
+
const transportMod = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
269
|
+
const SdkClient = clientMod.Client;
|
|
270
|
+
const SdkTransport = transportMod.StreamableHTTPClientTransport;
|
|
271
|
+
const transport = new SdkTransport(url, { requestInit });
|
|
272
|
+
const client = new SdkClient({ name: clientName, version: "1.0.0" }, { capabilities: {} });
|
|
273
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
274
|
+
await client.connect(transport);
|
|
275
|
+
return new SdkMcpClient(client, transport);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
// SDK transport failed or not available — fall back to raw JSON-RPC.
|
|
279
|
+
}
|
|
280
|
+
// --- Attempt 2: Raw JSON-RPC 2.0 ---
|
|
281
|
+
const headers = {};
|
|
282
|
+
if (options.authorization) {
|
|
283
|
+
headers["Authorization"] = options.authorization;
|
|
284
|
+
}
|
|
285
|
+
const rawClient = new RawMcpClient(options.url, headers);
|
|
286
|
+
await rawClient.initialize(clientName);
|
|
287
|
+
return rawClient;
|
|
288
|
+
}
|
|
289
|
+
//# sourceMappingURL=mcp-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../src/mcp-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,oEAAoE;AACpE,4DAA4D;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAiC1C,8EAA8E;AAC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C,2CAA2C,CAAC,CAAC;QAC5E,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,oDAAoD,CAAC,CAAC;QACxF,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC;QACnC,MAAM,YAAY,GAAG,YAAY,CAAC,6BAA6B,CAAC;QAEhE,MAAM,SAAS,GAAG,IAAI,YAAY,CAAC,GAAG,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;QACzD,MAAM,MAAM,GAAG,IAAI,SAAS,CAC1B,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,EACtC,EAAE,YAAY,EAAE,EAAE,EAAE,CACrB,CAAC;QACF,8DAA8D;QAC9D,MAAM,MAAM,CAAC,OAAO,CAAC,SAAgB,CAAC,CAAC;QACvC,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,qEAAqE;IACvE,CAAC;IAED,sCAAsC;IACtC,MAAM,OAAO,GAA2B,EAAE,CAAC;IAC3C,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;QAC1B,OAAO,CAAC,eAAe,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC;IACnD,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACzD,MAAM,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;IACvC,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/dist/mcp.d.ts
CHANGED
|
@@ -47,4 +47,27 @@ export declare function createMcpServer(config: AgentConfig, routes: RouteRegist
|
|
|
47
47
|
* that communicates over stdin/stdout.
|
|
48
48
|
*/
|
|
49
49
|
export declare function serveMcpStdio(server: McpServer): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a Streamable HTTP handler for an MCP server.
|
|
52
|
+
*
|
|
53
|
+
* The MCP specification (2025-03-26) introduced Streamable HTTP as the
|
|
54
|
+
* recommended remote transport, deprecating the older SSE transport.
|
|
55
|
+
* This function returns a `(req: Request) => Promise<Response>` handler
|
|
56
|
+
* that can be mounted in any web-standard framework (Hono, Cloudflare
|
|
57
|
+
* Workers, Deno, Bun, etc.).
|
|
58
|
+
*
|
|
59
|
+
* The handler supports:
|
|
60
|
+
* - **POST** — JSON-RPC messages (tool calls, initialization)
|
|
61
|
+
* - **GET** — SSE stream for server-initiated notifications
|
|
62
|
+
* - **DELETE** — session termination
|
|
63
|
+
*
|
|
64
|
+
* Session management is enabled by default using `crypto.randomUUID()`.
|
|
65
|
+
* Each session maintains its own transport instance, which is connected
|
|
66
|
+
* to a freshly built `McpServer` that shares the same tool registrations.
|
|
67
|
+
*
|
|
68
|
+
* @param registry - The CapabilityRegistry (or equivalent) providing
|
|
69
|
+
* agent configuration and registered routes.
|
|
70
|
+
* @returns A web-standard request handler.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createMcpHttpHandler(config: AgentConfig, routes: RouteRegistryEntry[], executeRoute: (method: string, path: string, input: unknown) => Promise<unknown>): (req: Request) => Promise<Response>;
|
|
50
73
|
//# sourceMappingURL=mcp.d.ts.map
|
package/dist/mcp.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAGpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAGpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC,OAAO,KAA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