@dudko.dev/agent-web 0.0.38 → 0.0.39

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/mcp.d.cts CHANGED
@@ -6,6 +6,34 @@ import { ToolSet } from 'ai';
6
6
  import { OAuthClientMetadata, OAuthClientInformationMixed, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
7
7
  import { VaultOptions } from './index.cjs';
8
8
 
9
+ /**
10
+ * What the MCP connectors of the two agent packages share: how a server's `instructions` reach a
11
+ * system prompt, and how a tool result's `structuredContent` reaches the model. Pure, no imports.
12
+ *
13
+ * This file is a COPY, byte for byte, in `@dudko.dev/agent` (`src/mcp-shared.ts`) and
14
+ * `@dudko.dev/agent-web` (`src/mcp/shared.ts`): change both. The Node package's
15
+ * `tests/mcp-shared.test.ts` compares the two files and tests every cap and guard here.
16
+ */
17
+ /** The `instructions` one connected MCP server returned in its `initialize` result. */
18
+ interface McpServerInstructions {
19
+ /** The server's name: its key in the host's configuration. */
20
+ server: string;
21
+ instructions: string;
22
+ }
23
+ /** The most one server's instructions take in a system prompt, in characters. */
24
+ declare const MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER = 2000;
25
+ /** The most all servers' instructions take together, in characters. */
26
+ declare const MCP_INSTRUCTIONS_MAX_CHARS_TOTAL = 8000;
27
+ /**
28
+ * The system-prompt section for the servers' instructions, or '' when no server sent any (the
29
+ * prompt is then exactly what it was). Each server's text is capped
30
+ * (MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER), so is the sum (MCP_INSTRUCTIONS_MAX_CHARS_TOTAL, in
31
+ * the order given: a server past the total is named as omitted), and every line of it is
32
+ * prefixed with "| ", so a server cannot write something that reads as a heading of this prompt
33
+ * or as another server's note.
34
+ */
35
+ declare const renderMcpInstructions: (list: readonly McpServerInstructions[] | undefined) => string;
36
+
9
37
  /**
10
38
  * A remote MCP server reached over StreamableHTTP. Browsers can ONLY speak the
11
39
  * HTTP transport — stdio (child processes) is Node-only and intentionally
@@ -63,6 +91,15 @@ interface ConnectedMcp {
63
91
  catalog: McpCatalogEntry[];
64
92
  /** Per-server connect outcome. */
65
93
  results: McpServerResult[];
94
+ /**
95
+ * The `instructions` each connected server returned in its `initialize`
96
+ * result, in declaration order; servers that sent none are absent. Read once
97
+ * per connection. Third-party text: the agent shows it to the model under a
98
+ * preamble that says so (`config.mcpInstructions`, on by default) — each
99
+ * mounted tool carries its server's instructions, so merging `tools` into
100
+ * the agent's config is all a host does.
101
+ */
102
+ instructions: McpServerInstructions[];
66
103
  /**
67
104
  * Re-list one server's tools and rewrite its entries in `tools`/`catalog`.
68
105
  * Both are mutated in place, so anything holding a reference (including a
@@ -624,5 +661,5 @@ declare class BrowserEnterpriseOAuthProvider implements OAuthClientProvider {
624
661
  /** Convenience factory mirroring the rest of the package's `createX` style. */
625
662
  declare const createEnterpriseOAuthProvider: (opts: EnterpriseOAuthProviderOptions) => BrowserEnterpriseOAuthProvider;
626
663
 
627
- export { BrowserEnterpriseOAuthProvider, BrowserOAuthProvider, ENTERPRISE_MANAGED_AUTHORIZATION_EXTENSION, EnterpriseOAuthError, MemoryOAuthStorage, VaultOAuthStorage, beginMcpOAuth, checkAuthorizationResponseIssuer, connectMcpHttp, createEnterpriseOAuthProvider, diagnoseMcpCors, finishMcpOAuth, flattenContent, readOAuthCallback };
628
- export type { BrowserOAuthProviderOptions, ConnectMcpOptions, ConnectedMcp, EnterpriseAssertionAlgorithm, EnterpriseAssertionAudience, EnterpriseAssertionType, EnterpriseIdpOptions, EnterpriseOAuthHop, EnterpriseOAuthLog, EnterpriseOAuthProviderOptions, EnterprisePrivateKey, McpCatalogEntry, McpHttpServerConfig, McpOAuthCallback, McpOAuthRequestOptions, McpOAuthStorage, McpServerResult };
664
+ export { BrowserEnterpriseOAuthProvider, BrowserOAuthProvider, ENTERPRISE_MANAGED_AUTHORIZATION_EXTENSION, EnterpriseOAuthError, MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER, MCP_INSTRUCTIONS_MAX_CHARS_TOTAL, MemoryOAuthStorage, VaultOAuthStorage, beginMcpOAuth, checkAuthorizationResponseIssuer, connectMcpHttp, createEnterpriseOAuthProvider, diagnoseMcpCors, finishMcpOAuth, flattenContent, readOAuthCallback, renderMcpInstructions };
665
+ export type { BrowserOAuthProviderOptions, ConnectMcpOptions, ConnectedMcp, EnterpriseAssertionAlgorithm, EnterpriseAssertionAudience, EnterpriseAssertionType, EnterpriseIdpOptions, EnterpriseOAuthHop, EnterpriseOAuthLog, EnterpriseOAuthProviderOptions, EnterprisePrivateKey, McpCatalogEntry, McpHttpServerConfig, McpOAuthCallback, McpOAuthRequestOptions, McpOAuthStorage, McpServerInstructions, McpServerResult };
package/dist/mcp.d.ts CHANGED
@@ -6,6 +6,34 @@ import { ToolSet } from 'ai';
6
6
  import { OAuthClientMetadata, OAuthClientInformationMixed, OAuthTokens } from '@modelcontextprotocol/sdk/shared/auth.js';
7
7
  import { VaultOptions } from './index.js';
8
8
 
9
+ /**
10
+ * What the MCP connectors of the two agent packages share: how a server's `instructions` reach a
11
+ * system prompt, and how a tool result's `structuredContent` reaches the model. Pure, no imports.
12
+ *
13
+ * This file is a COPY, byte for byte, in `@dudko.dev/agent` (`src/mcp-shared.ts`) and
14
+ * `@dudko.dev/agent-web` (`src/mcp/shared.ts`): change both. The Node package's
15
+ * `tests/mcp-shared.test.ts` compares the two files and tests every cap and guard here.
16
+ */
17
+ /** The `instructions` one connected MCP server returned in its `initialize` result. */
18
+ interface McpServerInstructions {
19
+ /** The server's name: its key in the host's configuration. */
20
+ server: string;
21
+ instructions: string;
22
+ }
23
+ /** The most one server's instructions take in a system prompt, in characters. */
24
+ declare const MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER = 2000;
25
+ /** The most all servers' instructions take together, in characters. */
26
+ declare const MCP_INSTRUCTIONS_MAX_CHARS_TOTAL = 8000;
27
+ /**
28
+ * The system-prompt section for the servers' instructions, or '' when no server sent any (the
29
+ * prompt is then exactly what it was). Each server's text is capped
30
+ * (MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER), so is the sum (MCP_INSTRUCTIONS_MAX_CHARS_TOTAL, in
31
+ * the order given: a server past the total is named as omitted), and every line of it is
32
+ * prefixed with "| ", so a server cannot write something that reads as a heading of this prompt
33
+ * or as another server's note.
34
+ */
35
+ declare const renderMcpInstructions: (list: readonly McpServerInstructions[] | undefined) => string;
36
+
9
37
  /**
10
38
  * A remote MCP server reached over StreamableHTTP. Browsers can ONLY speak the
11
39
  * HTTP transport — stdio (child processes) is Node-only and intentionally
@@ -63,6 +91,15 @@ interface ConnectedMcp {
63
91
  catalog: McpCatalogEntry[];
64
92
  /** Per-server connect outcome. */
65
93
  results: McpServerResult[];
94
+ /**
95
+ * The `instructions` each connected server returned in its `initialize`
96
+ * result, in declaration order; servers that sent none are absent. Read once
97
+ * per connection. Third-party text: the agent shows it to the model under a
98
+ * preamble that says so (`config.mcpInstructions`, on by default) — each
99
+ * mounted tool carries its server's instructions, so merging `tools` into
100
+ * the agent's config is all a host does.
101
+ */
102
+ instructions: McpServerInstructions[];
66
103
  /**
67
104
  * Re-list one server's tools and rewrite its entries in `tools`/`catalog`.
68
105
  * Both are mutated in place, so anything holding a reference (including a
@@ -624,5 +661,5 @@ declare class BrowserEnterpriseOAuthProvider implements OAuthClientProvider {
624
661
  /** Convenience factory mirroring the rest of the package's `createX` style. */
625
662
  declare const createEnterpriseOAuthProvider: (opts: EnterpriseOAuthProviderOptions) => BrowserEnterpriseOAuthProvider;
626
663
 
627
- export { BrowserEnterpriseOAuthProvider, BrowserOAuthProvider, ENTERPRISE_MANAGED_AUTHORIZATION_EXTENSION, EnterpriseOAuthError, MemoryOAuthStorage, VaultOAuthStorage, beginMcpOAuth, checkAuthorizationResponseIssuer, connectMcpHttp, createEnterpriseOAuthProvider, diagnoseMcpCors, finishMcpOAuth, flattenContent, readOAuthCallback };
628
- export type { BrowserOAuthProviderOptions, ConnectMcpOptions, ConnectedMcp, EnterpriseAssertionAlgorithm, EnterpriseAssertionAudience, EnterpriseAssertionType, EnterpriseIdpOptions, EnterpriseOAuthHop, EnterpriseOAuthLog, EnterpriseOAuthProviderOptions, EnterprisePrivateKey, McpCatalogEntry, McpHttpServerConfig, McpOAuthCallback, McpOAuthRequestOptions, McpOAuthStorage, McpServerResult };
664
+ export { BrowserEnterpriseOAuthProvider, BrowserOAuthProvider, ENTERPRISE_MANAGED_AUTHORIZATION_EXTENSION, EnterpriseOAuthError, MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER, MCP_INSTRUCTIONS_MAX_CHARS_TOTAL, MemoryOAuthStorage, VaultOAuthStorage, beginMcpOAuth, checkAuthorizationResponseIssuer, connectMcpHttp, createEnterpriseOAuthProvider, diagnoseMcpCors, finishMcpOAuth, flattenContent, readOAuthCallback, renderMcpInstructions };
665
+ export type { BrowserOAuthProviderOptions, ConnectMcpOptions, ConnectedMcp, EnterpriseAssertionAlgorithm, EnterpriseAssertionAudience, EnterpriseAssertionType, EnterpriseIdpOptions, EnterpriseOAuthHop, EnterpriseOAuthLog, EnterpriseOAuthProviderOptions, EnterprisePrivateKey, McpCatalogEntry, McpHttpServerConfig, McpOAuthCallback, McpOAuthRequestOptions, McpOAuthStorage, McpServerInstructions, McpServerResult };
package/dist/mcp.js CHANGED
@@ -1,7 +1,16 @@
1
1
  import {
2
2
  IndexedDBVault,
3
- createLogger
4
- } from "./chunk-T6LZW7ML.js";
3
+ MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER,
4
+ MCP_INSTRUCTIONS_MAX_CHARS_TOTAL,
5
+ cleanMcpInstructions,
6
+ createLogger,
7
+ dropStructuredDuplicates,
8
+ isStructuredContent,
9
+ markMcpTool,
10
+ outputSchemaNote,
11
+ renderMcpInstructions,
12
+ withStructuredContent
13
+ } from "./chunk-A6ZJALLU.js";
5
14
  import "./chunk-CTXDKSDD.js";
6
15
 
7
16
  // src/mcp/http.ts
@@ -13,7 +22,10 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
13
22
  import {
14
23
  StreamableHTTPClientTransport
15
24
  } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
16
- import { ToolListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
25
+ import {
26
+ CallToolResultSchema,
27
+ ToolListChangedNotificationSchema
28
+ } from "@modelcontextprotocol/sdk/types.js";
17
29
  import { dynamicTool, jsonSchema } from "ai";
18
30
 
19
31
  // src/mcp/oauth.ts
@@ -370,6 +382,10 @@ var flattenContent = (content) => {
370
382
  if (allText) return parts.map((p) => p.text).join("\n");
371
383
  return parts.map((p) => p?.type === "text" && typeof p.text === "string" ? p.text : p);
372
384
  };
385
+ var capturingResultSchema = (onResult) => CallToolResultSchema.refine((result) => {
386
+ onResult(result);
387
+ return true;
388
+ });
373
389
  var asEnterpriseProvider = (provider) => {
374
390
  const p = provider;
375
391
  return typeof p.extension === "string" && typeof p.authorize === "function" && typeof p.transportFetch === "function" ? provider : void 0;
@@ -524,7 +540,7 @@ var connectAndList = async (name, cfg, opts, log, onClient) => {
524
540
  client.setNotificationHandler(ToolListChangedNotificationSchema, () => notify(name));
525
541
  }
526
542
  const listed = await listAllTools(client);
527
- return { name, client, listed };
543
+ return { name, client, listed, instructions: cleanMcpInstructions(client.getInstructions()) };
528
544
  };
529
545
  var connectMcpHttp = async (servers, opts = {}) => {
530
546
  const log = opts.onLog ?? (() => {
@@ -533,6 +549,8 @@ var connectMcpHttp = async (servers, opts = {}) => {
533
549
  const tools = {};
534
550
  const catalog = [];
535
551
  const results = [];
552
+ const instructions = [];
553
+ const instructionsOf = /* @__PURE__ */ new Map();
536
554
  const mountedKeys = /* @__PURE__ */ new Map();
537
555
  const mountTools = (name, client, listed) => {
538
556
  const prefix = `${sanitizeName(name)}__`;
@@ -567,19 +585,39 @@ var connectMcpHttp = async (servers, opts = {}) => {
567
585
  description,
568
586
  inputSchema: jsonSchema(t.inputSchema),
569
587
  execute: async (args, options) => {
570
- const res = await client.callTool(
571
- { name: t.name, arguments: args ?? {} },
572
- void 0,
573
- options?.abortSignal ? { signal: options.abortSignal } : void 0
574
- );
575
- const flat = flattenContent(res.content);
588
+ let res;
589
+ let schemaNote;
590
+ let received;
591
+ try {
592
+ res = await client.callTool(
593
+ { name: t.name, arguments: args ?? {} },
594
+ capturingResultSchema((r) => {
595
+ received = r;
596
+ }),
597
+ options?.abortSignal ? { signal: options.abortSignal } : void 0
598
+ );
599
+ } catch (err) {
600
+ if (!received) throw err;
601
+ res = received;
602
+ schemaNote = outputSchemaNote(t.name, err);
603
+ log(
604
+ "warn",
605
+ `[mcp] ${key}: result does not follow the declared outputSchema - ${err instanceof Error ? err.message : String(err)}; passed on with a note`
606
+ );
607
+ }
576
608
  if (res.isError) {
577
- throw new Error(typeof flat === "string" ? flat : JSON.stringify(flat));
609
+ const flat2 = flattenContent(res.content);
610
+ throw new Error(typeof flat2 === "string" ? flat2 : JSON.stringify(flat2));
578
611
  }
579
- return flat;
612
+ const structured = isStructuredContent(res.structuredContent) ? res.structuredContent : void 0;
613
+ const flat = flattenContent(
614
+ structured ? dropStructuredDuplicates(res.content, structured) : res.content
615
+ );
616
+ return withStructuredContent(flat, structured, schemaNote);
580
617
  }
581
618
  });
582
619
  if (readOnly) tools[key].readOnly = true;
620
+ markMcpTool(tools[key], name, instructionsOf.get(name));
583
621
  catalog.push({ name: key, description, server: name, ...readOnly ? { readOnly } : {} });
584
622
  keys.push(key);
585
623
  mounted += 1;
@@ -601,6 +639,14 @@ var connectMcpHttp = async (servers, opts = {}) => {
601
639
  continue;
602
640
  }
603
641
  clients.set(server.name, server.client);
642
+ if (server.instructions) {
643
+ log(
644
+ "info",
645
+ `[mcp] ${server.name}: the server sent instructions (${server.instructions.length} chars)`
646
+ );
647
+ instructions.push({ server: server.name, instructions: server.instructions });
648
+ instructionsOf.set(server.name, server.instructions);
649
+ }
604
650
  mountTools(server.name, server.client, server.listed);
605
651
  results.push({ name: server.name, connected: true });
606
652
  }
@@ -608,6 +654,7 @@ var connectMcpHttp = async (servers, opts = {}) => {
608
654
  tools,
609
655
  catalog,
610
656
  results,
657
+ instructions,
611
658
  refreshServer: async (name) => {
612
659
  const client = clients.get(name);
613
660
  if (!client) throw new Error(`MCP server "${name}" is not connected`);
@@ -1369,6 +1416,8 @@ export {
1369
1416
  BrowserOAuthProvider,
1370
1417
  ENTERPRISE_MANAGED_AUTHORIZATION_EXTENSION,
1371
1418
  EnterpriseOAuthError,
1419
+ MCP_INSTRUCTIONS_MAX_CHARS_PER_SERVER,
1420
+ MCP_INSTRUCTIONS_MAX_CHARS_TOTAL,
1372
1421
  MemoryOAuthStorage,
1373
1422
  UnauthorizedError2 as UnauthorizedError,
1374
1423
  VaultOAuthStorage,
@@ -1379,6 +1428,7 @@ export {
1379
1428
  diagnoseMcpCors,
1380
1429
  finishMcpOAuth,
1381
1430
  flattenContent,
1382
- readOAuthCallback
1431
+ readOAuthCallback,
1432
+ renderMcpInstructions
1383
1433
  };
1384
1434
  //# sourceMappingURL=mcp.js.map