stitchkit 0.0.1 → 0.2.0

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.
Files changed (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +376 -4
  3. package/dist/browser/client.d.ts +30 -0
  4. package/dist/browser/client.d.ts.map +1 -0
  5. package/dist/browser/http.d.ts +77 -0
  6. package/dist/browser/http.d.ts.map +1 -0
  7. package/dist/browser/socket-io.d.ts +69 -0
  8. package/dist/browser/socket-io.d.ts.map +1 -0
  9. package/dist/browser/stream.d.ts +2 -0
  10. package/dist/browser/stream.d.ts.map +1 -0
  11. package/dist/contract/define.d.ts +94 -0
  12. package/dist/contract/define.d.ts.map +1 -0
  13. package/dist/contract/errors.d.ts +47 -0
  14. package/dist/contract/errors.d.ts.map +1 -0
  15. package/dist/contract/index.d.ts +4 -0
  16. package/dist/contract/index.d.ts.map +1 -0
  17. package/dist/contract/index.js +26 -0
  18. package/dist/contract/pagination.d.ts +19 -0
  19. package/dist/contract/pagination.d.ts.map +1 -0
  20. package/dist/index-5sxnvwb1.js +368 -0
  21. package/dist/index-809wc1tt.js +18 -0
  22. package/dist/index-a35v22fh.js +29 -0
  23. package/dist/index-kckky6zw.js +96 -0
  24. package/dist/index-ke4mx4ea.js +38 -0
  25. package/dist/index-n7bmdwmz.js +68 -0
  26. package/dist/index-v2z2v3mq.js +587 -0
  27. package/dist/index.d.ts +7 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +111 -0
  30. package/dist/internal/errors.d.ts +5 -0
  31. package/dist/internal/errors.d.ts.map +1 -0
  32. package/dist/internal/typed.d.ts +9 -0
  33. package/dist/internal/typed.d.ts.map +1 -0
  34. package/dist/node.d.ts +4 -0
  35. package/dist/node.d.ts.map +1 -0
  36. package/dist/node.js +26 -0
  37. package/dist/observability/audit.d.ts +32 -0
  38. package/dist/observability/audit.d.ts.map +1 -0
  39. package/dist/observability/context.d.ts +60 -0
  40. package/dist/observability/context.d.ts.map +1 -0
  41. package/dist/observability/event.d.ts +49 -0
  42. package/dist/observability/event.d.ts.map +1 -0
  43. package/dist/observability/index.d.ts +14 -0
  44. package/dist/observability/index.d.ts.map +1 -0
  45. package/dist/observability/index.js +266 -0
  46. package/dist/observability/sanitize.d.ts +42 -0
  47. package/dist/observability/sanitize.d.ts.map +1 -0
  48. package/dist/observability/trace.d.ts +42 -0
  49. package/dist/observability/trace.d.ts.map +1 -0
  50. package/dist/react/cache-bridge.d.ts +53 -0
  51. package/dist/react/cache-bridge.d.ts.map +1 -0
  52. package/dist/react/cursor-query.d.ts +37 -0
  53. package/dist/react/cursor-query.d.ts.map +1 -0
  54. package/dist/react.d.ts +3 -0
  55. package/dist/react.d.ts.map +1 -0
  56. package/dist/react.js +52 -0
  57. package/dist/server/cache.d.ts +15 -0
  58. package/dist/server/cache.d.ts.map +1 -0
  59. package/dist/server/context.d.ts +9 -0
  60. package/dist/server/context.d.ts.map +1 -0
  61. package/dist/server/create.d.ts +4 -0
  62. package/dist/server/create.d.ts.map +1 -0
  63. package/dist/server/event-bus.d.ts +24 -0
  64. package/dist/server/event-bus.d.ts.map +1 -0
  65. package/dist/server/implement.d.ts +16 -0
  66. package/dist/server/implement.d.ts.map +1 -0
  67. package/dist/server/index.d.ts +17 -0
  68. package/dist/server/index.d.ts.map +1 -0
  69. package/dist/server/index.js +399 -0
  70. package/dist/server/logger.d.ts +21 -0
  71. package/dist/server/logger.d.ts.map +1 -0
  72. package/dist/server/middleware/auth.d.ts +50 -0
  73. package/dist/server/middleware/auth.d.ts.map +1 -0
  74. package/dist/server/middleware/cookies.d.ts +34 -0
  75. package/dist/server/middleware/cookies.d.ts.map +1 -0
  76. package/dist/server/middleware/cors.d.ts +9 -0
  77. package/dist/server/middleware/cors.d.ts.map +1 -0
  78. package/dist/server/multipart.d.ts +14 -0
  79. package/dist/server/multipart.d.ts.map +1 -0
  80. package/dist/server/node.d.ts +12 -0
  81. package/dist/server/node.d.ts.map +1 -0
  82. package/dist/server/rate-limit.d.ts +17 -0
  83. package/dist/server/rate-limit.d.ts.map +1 -0
  84. package/dist/server/request.d.ts +22 -0
  85. package/dist/server/request.d.ts.map +1 -0
  86. package/dist/server/router.d.ts +42 -0
  87. package/dist/server/router.d.ts.map +1 -0
  88. package/dist/server/socket-io.d.ts +42 -0
  89. package/dist/server/socket-io.d.ts.map +1 -0
  90. package/dist/server/stream.d.ts +18 -0
  91. package/dist/server/stream.d.ts.map +1 -0
  92. package/dist/server/swept-map.d.ts +22 -0
  93. package/dist/server/swept-map.d.ts.map +1 -0
  94. package/dist/server/types.d.ts +127 -0
  95. package/dist/server/types.d.ts.map +1 -0
  96. package/dist/tools/agent.d.ts +27 -0
  97. package/dist/tools/agent.d.ts.map +1 -0
  98. package/dist/tools/coerce.d.ts +8 -0
  99. package/dist/tools/coerce.d.ts.map +1 -0
  100. package/dist/tools/execute.d.ts +45 -0
  101. package/dist/tools/execute.d.ts.map +1 -0
  102. package/dist/tools/flatten.d.ts +15 -0
  103. package/dist/tools/flatten.d.ts.map +1 -0
  104. package/dist/tools/json-schema.d.ts +18 -0
  105. package/dist/tools/json-schema.d.ts.map +1 -0
  106. package/dist/tools/manifest.d.ts +13 -0
  107. package/dist/tools/manifest.d.ts.map +1 -0
  108. package/dist/tools/mcp-handler.d.ts +21 -0
  109. package/dist/tools/mcp-handler.d.ts.map +1 -0
  110. package/dist/tools/mcp-stdio.d.ts +25 -0
  111. package/dist/tools/mcp-stdio.d.ts.map +1 -0
  112. package/dist/tools/mcp.d.ts +92 -0
  113. package/dist/tools/mcp.d.ts.map +1 -0
  114. package/dist/tools/mount.d.ts +73 -0
  115. package/dist/tools/mount.d.ts.map +1 -0
  116. package/dist/tools/names.d.ts +2 -0
  117. package/dist/tools/names.d.ts.map +1 -0
  118. package/dist/tools/remote.d.ts +22 -0
  119. package/dist/tools/remote.d.ts.map +1 -0
  120. package/dist/tools/schema.d.ts +21 -0
  121. package/dist/tools/schema.d.ts.map +1 -0
  122. package/dist/tools/view-file.d.ts +57 -0
  123. package/dist/tools/view-file.d.ts.map +1 -0
  124. package/dist/tools.d.ts +12 -0
  125. package/dist/tools.d.ts.map +1 -0
  126. package/dist/tools.js +837 -0
  127. package/package.json +147 -4
@@ -0,0 +1,13 @@
1
+ import type { MountableTool } from './mount';
2
+ export interface ToolManifestEntry {
3
+ name: string;
4
+ description: string;
5
+ inputSchema: Record<string, unknown>;
6
+ }
7
+ /**
8
+ * Build a searchable manifest from collected tools — name, description and
9
+ * JSON Schema for each. Use it to power a `tool_search` native tool: the app
10
+ * decides the search algorithm and the unlock mechanism.
11
+ */
12
+ export declare function buildToolManifest(tools: MountableTool[]): ToolManifestEntry[];
13
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../src/tools/manifest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,aAAa,EAAE,GAAG,iBAAiB,EAAE,CAM7E"}
@@ -0,0 +1,21 @@
1
+ import { type McpServerBuildConfig } from './mcp';
2
+ export interface McpHandlerConfig<TAuth> extends McpServerBuildConfig<TAuth> {
3
+ /** Resolve an incoming request to an identity. Return `null` → 401. */
4
+ auth: (req: Request) => TAuth | null | Promise<TAuth | null>;
5
+ }
6
+ /**
7
+ * Build a Streamable-HTTP MCP request handler (`Request → Response`).
8
+ *
9
+ * Owns the entire MCP server lifecycle — SSE event store, per-session
10
+ * transports, the `McpServer` instances — so the consuming app never imports
11
+ * `@modelcontextprotocol/sdk` itself. The app only declares WHAT to expose:
12
+ * how to authenticate and which contract services. MCP tools come from
13
+ * contracts; native multimodal tools attach via `nativeTools`.
14
+ *
15
+ * The server itself is built by the transport-neutral `buildMcpServer` — the
16
+ * same core used by `createStdioMcpServer`.
17
+ *
18
+ * Mount the returned handler in your server's fetch router (e.g. under `/mcp`).
19
+ */
20
+ export declare function createMcpHandler<TAuth>(config: McpHandlerConfig<TAuth>): (req: Request) => Promise<Response>;
21
+ //# sourceMappingURL=mcp-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-handler.d.ts","sourceRoot":"","sources":["../../src/tools/mcp-handler.ts"],"names":[],"mappings":"AASA,OAAO,EAAkB,KAAK,oBAAoB,EAAsB,MAAM,OAAO,CAAC;AAyDtF,MAAM,WAAW,gBAAgB,CAAC,KAAK,CAAE,SAAQ,oBAAoB,CAAC,KAAK,CAAC;IAC1E,uEAAuE;IACvE,IAAI,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,KAAK,GAAG,IAAI,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;CAC9D;AAYD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EACpC,MAAM,EAAE,gBAAgB,CAAC,KAAK,CAAC,GAC9B,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CA+DrC"}
@@ -0,0 +1,25 @@
1
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { type McpServerBuildConfig } from './mcp';
3
+ /**
4
+ * Config for a stdio MCP server. Unlike the HTTP handler, a stdio server is a
5
+ * single process serving one client — identity is resolved ONCE at startup
6
+ * (from an env var / CLI arg), not per request.
7
+ */
8
+ export interface StdioMcpServerConfig<TAuth> extends McpServerBuildConfig<TAuth> {
9
+ /** Identity for the single stdio session — a value or a promise of one. */
10
+ auth: TAuth | Promise<TAuth>;
11
+ }
12
+ /**
13
+ * Build an MCP server and connect it over stdio — the server runs as a
14
+ * subprocess of the MCP client, on the client's machine, so it can reach the
15
+ * client's local filesystem.
16
+ *
17
+ * Same contract pipeline as `createMcpHandler` (`buildMcpServer`); only the
18
+ * transport differs. For a local CLI: resolve the identity from `process.env`
19
+ * and pass it as `auth`.
20
+ *
21
+ * Note: stdout is reserved for the JSON-RPC stream — the caller MUST keep all
22
+ * logging on stderr (`console.error`), never `console.log`.
23
+ */
24
+ export declare function createStdioMcpServer<TAuth>(config: StdioMcpServerConfig<TAuth>): Promise<McpServer>;
25
+ //# sourceMappingURL=mcp-stdio.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-stdio.d.ts","sourceRoot":"","sources":["../../src/tools/mcp-stdio.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAEzE,OAAO,EAAkB,KAAK,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAElE;;;;GAIG;AACH,MAAM,WAAW,oBAAoB,CAAC,KAAK,CAAE,SAAQ,oBAAoB,CAAC,KAAK,CAAC;IAC9E,2EAA2E;IAC3E,IAAI,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;CAC9B;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,oBAAoB,CAAC,KAAK,EAC9C,MAAM,EAAE,oBAAoB,CAAC,KAAK,CAAC,GAClC,OAAO,CAAC,SAAS,CAAC,CAKpB"}
@@ -0,0 +1,92 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import type { ServiceDef, StitchLogger } from '../server/types';
3
+ import { type ToolCallHooks, type ToolLifecycle } from './execute';
4
+ import { type ToolExtend } from './mount';
5
+ /**
6
+ * What to do when a tool's schema cannot be advertised on the MCP surface — a
7
+ * union / discriminated-union input (MCP needs an object), or a construct
8
+ * JSON Schema cannot represent (`z.date()`, `z.map()`, …):
9
+ * - `throw` (default) — fail the build loudly, listing every bad tool. Better
10
+ * a failed deploy than a tool that silently vanishes from the MCP surface.
11
+ * - `warn` — log and drop the tool.
12
+ * - `skip` — drop the tool silently.
13
+ */
14
+ export type IncompatibleSchemaPolicy = 'throw' | 'skip' | 'warn';
15
+ export interface McpMountConfig {
16
+ context?: Record<string, unknown>;
17
+ /** Tool-call observability hooks. */
18
+ hooks?: ToolCallHooks;
19
+ /**
20
+ * Auth / scope gate and result transform for every tool call — the tool-side
21
+ * twin of `createServer`'s `beforeHandle` / `afterHandle`. Pass the same
22
+ * `createAuthHook` result here as on the HTTP server to guard tools too;
23
+ * without it a tool call bypasses the HTTP `beforeHandle` auth gate.
24
+ */
25
+ lifecycle?: ToolLifecycle;
26
+ extend?: ToolExtend;
27
+ /** What to do when a tool's schema is not MCP-compatible. Default `'throw'`. */
28
+ onIncompatibleSchema?: IncompatibleSchemaPolicy;
29
+ /** Logger for the `'warn'` policy — defaults to `console`. */
30
+ logger?: StitchLogger;
31
+ /** Coerce JSON-stringified arrays/objects in tool arguments. Default: true. */
32
+ coerceJsonArgs?: boolean;
33
+ /** Flatten discriminated union inputs into a single object. Default: false. */
34
+ flattenUnionInput?: boolean;
35
+ /** Global error hint injected into every failed tool result. */
36
+ errorHint?: (toolName: string, errorCode: string) => string | null;
37
+ }
38
+ /**
39
+ * Validate that every contract tool in `services` can be advertised on the MCP
40
+ * surface — object-shaped, JSON Schema-compatible input / output, no tool-name
41
+ * collision across services. The build-time check behind `mountMcp` — also
42
+ * callable on its own (a startup assertion, a test) to fail a deploy before
43
+ * the first request.
44
+ */
45
+ export declare function validateMcpSchemas(services: ServiceDef[], onIncompatibleSchema?: IncompatibleSchemaPolicy, logger?: StitchLogger): void;
46
+ export declare function mountMcp(mcpServer: McpServer, services: ServiceDef | ServiceDef[], config?: McpMountConfig): void;
47
+ /**
48
+ * Transport-neutral build config for an MCP server — everything needed to turn
49
+ * contract services into a live `McpServer`, minus how the identity is
50
+ * resolved. `createMcpHandler` (HTTP) and `createStdioMcpServer` (stdio) each
51
+ * add their own `auth` on top.
52
+ */
53
+ export interface McpServerBuildConfig<TAuth> {
54
+ /** MCP server identity (name + version). */
55
+ serverInfo: {
56
+ name: string;
57
+ version: string;
58
+ };
59
+ /** Contract services exposed as MCP tools — may depend on the identity. */
60
+ services: ServiceDef[] | ((auth: TAuth) => ServiceDef[]);
61
+ /** Context merged into every contract handler (`mountMcp` context). */
62
+ context?: (auth: TAuth) => Record<string, unknown>;
63
+ /** Tool-call observability hooks — `afterToolCall` fires for every result
64
+ * (success and error), so the consuming app can log MCP tool outcomes. */
65
+ hooks?: ToolCallHooks;
66
+ /** Auth / scope gate and result transform for every tool call — pass the
67
+ * same `createAuthHook` result used for the HTTP `beforeHandle` to guard
68
+ * tools with the identical rules. */
69
+ lifecycle?: ToolLifecycle;
70
+ /** What to do when a tool's schema is not MCP-compatible. Default `'throw'`. */
71
+ onIncompatibleSchema?: IncompatibleSchemaPolicy;
72
+ /** Logger for schema-incompatibility warnings — defaults to `console`. */
73
+ logger?: StitchLogger;
74
+ /** Register native (non-contract) MCP tools — receives the `McpServer`
75
+ * directly. For tools returning multimodal content, e.g. `mountViewFile`. */
76
+ nativeTools?: (server: McpServer) => void;
77
+ /** Server instructions — a short (≤2KB) hint to the host on when and how to
78
+ * use these tools. Surfaced to MCP tool-search. */
79
+ instructions?: string;
80
+ /** Coerce JSON-stringified arrays/objects in tool arguments. Default: true. */
81
+ coerceJsonArgs?: boolean;
82
+ /** Flatten discriminated union inputs into a single object. Default: false. */
83
+ flattenUnionInput?: boolean;
84
+ /** Global error hint injected into every failed tool result. */
85
+ errorHint?: (toolName: string, errorCode: string) => string | null;
86
+ }
87
+ /**
88
+ * Build an `McpServer` from contract services for a resolved identity.
89
+ * Transport-agnostic — the shared core behind every MCP transport.
90
+ */
91
+ export declare function buildMcpServer<TAuth>(config: McpServerBuildConfig<TAuth>, auth: TAuth): McpServer;
92
+ //# sourceMappingURL=mcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/tools/mcp.ts"],"names":[],"mappings":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AAC,KAAK;IACzC,4CAA4C;IAC5C,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9C,2EAA2E;IAC3E,QAAQ,EAAE,UAAU,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,KAAK,UAAU,EAAE,CAAC,CAAC;IACzD,uEAAuE;IACvE,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnD;+EAC2E;IAC3E,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB;;0CAEsC;IACtC,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,gFAAgF;IAChF,oBAAoB,CAAC,EAAE,wBAAwB,CAAC;IAChD,0EAA0E;IAC1E,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB;kFAC8E;IAC9E,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,CAAC;IAC1C;wDACoD;IACpD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+EAA+E;IAC/E,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,+EAA+E;IAC/E,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,gEAAgE;IAChE,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;CACpE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAClC,MAAM,EAAE,oBAAoB,CAAC,KAAK,CAAC,EACnC,IAAI,EAAE,KAAK,GACV,SAAS,CAoBX"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Shared mounting machinery for MCP and agent tools. `mountMcp` and `mountAgent`
3
+ * differ only in their transport SDK — the method walk, the schema merge, the
4
+ * extend handling and the call execution are identical and live here.
5
+ */
6
+ import { z } from 'zod';
7
+ import type { Transport, TransportSource } from '../contract';
8
+ import type { MethodDef, ServiceDef } from '../server/types';
9
+ import { type ToolCallHooks, type ToolLifecycle, type ToolResult } from './execute';
10
+ /**
11
+ * Extra arguments folded into a mounted tool's schema — the host supplies them,
12
+ * `resolve` turns them into handler context. Shared by `mountMcp` / `mountAgent`.
13
+ */
14
+ export interface ToolExtend {
15
+ /** Extra Zod fields added to every (matching) tool's input schema. */
16
+ schema: Record<string, z.ZodType>;
17
+ /** Turn the extra arguments into context merged into the handler. */
18
+ resolve: (args: Record<string, unknown>) => Record<string, unknown> | Promise<Record<string, unknown>>;
19
+ /** Limit the extension to specific methods — default: every method. */
20
+ filter?: (service: ServiceDef, method: MethodDef) => boolean;
21
+ }
22
+ /** One contract method resolved for mounting as a tool. */
23
+ export interface MountableTool {
24
+ /** The contract method behind the tool. */
25
+ method: MethodDef<unknown, unknown, unknown>;
26
+ /** Tool name — the `toolName` override, else derived from service + method. */
27
+ name: string;
28
+ /** Merged params + input schema, with the extend fields folded in when they apply. */
29
+ schema: z.ZodType;
30
+ /** Whether `ToolExtend` applies to this method. */
31
+ shouldExtend: boolean;
32
+ }
33
+ export interface CollectToolsConfig {
34
+ extend?: ToolExtend;
35
+ /** Coerce JSON-stringified arrays/objects in tool arguments. Default: true. */
36
+ coerceJsonArgs?: boolean;
37
+ /** Flatten discriminated union inputs into a single object for MCP. Default: false. */
38
+ flattenUnionInput?: boolean;
39
+ }
40
+ /**
41
+ * Walk a service's methods and resolve each one exposed on `transport` to a
42
+ * tool name and schema — the shared front half of `mountMcp` / `mountAgent`.
43
+ */
44
+ export declare function collectTools(service: ServiceDef, transport: Transport, config?: CollectToolsConfig): MountableTool[];
45
+ /** Per-mount config a tool runner closes over. */
46
+ export interface ToolRunnerConfig {
47
+ /** Transport tag put on every call's context. */
48
+ source: TransportSource;
49
+ /** Extend applied at mount — its arguments are resolved then stripped. */
50
+ extend?: ToolExtend;
51
+ /** Static context merged into every handler. */
52
+ context?: Record<string, unknown>;
53
+ /** Tool-call observability hooks. */
54
+ hooks?: ToolCallHooks;
55
+ /** Auth / scope gate and result transform — runs for every tool call. */
56
+ lifecycle?: ToolLifecycle;
57
+ /** Global error hint injected into every failed tool result. */
58
+ errorHint?: (toolName: string, errorCode: string) => string | null;
59
+ }
60
+ /**
61
+ * Build the call executor for a mount — resolve the extend context, strip the
62
+ * extend arguments, execute. The shared back half of `mountMcp` / `mountAgent`.
63
+ */
64
+ export declare function createToolRunner(config: ToolRunnerConfig): (tool: MountableTool, rawArgs: Record<string, unknown>) => Promise<ToolResult>;
65
+ /**
66
+ * Shape a failed `ToolResult` into the `{ error, details?, _hint? }` object
67
+ * both transports return on error. When a global `errorHint` is provided, it
68
+ * is appended after the per-error `AppError.hint` (if any).
69
+ */
70
+ export declare function formatToolError(result: Extract<ToolResult, {
71
+ ok: false;
72
+ }>, toolName?: string, errorHint?: (toolName: string, errorCode: string) => string | null): Record<string, unknown>;
73
+ //# sourceMappingURL=mount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mount.d.ts","sourceRoot":"","sources":["../../src/tools/mount.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7D,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,UAAU,EAChB,MAAM,WAAW,CAAC;AAKnB;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;IAClC,qEAAqE;IACrE,OAAO,EAAE,CACP,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,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B,gBAAgB,CAC9B,MAAM,EAAE,gBAAgB,GACvB,CAAC,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAqBhF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE;IAAE,EAAE,EAAE,KAAK,CAAA;CAAE,CAAC,EAC1C,QAAQ,CAAC,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,GACjE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAazB"}
@@ -0,0 +1,2 @@
1
+ export declare function toToolName(serviceName: string, methodName: string): string;
2
+ //# sourceMappingURL=names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../../src/tools/names.ts"],"names":[],"mappings":"AAiBA,wBAAgB,UAAU,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAY1E"}
@@ -0,0 +1,22 @@
1
+ import type { HttpClient } from '../browser/http';
2
+ import type { ContractDef, EndpointDef } from '../contract';
3
+ import type { ServiceDef } from '../server/types';
4
+ export interface ImplementRemoteOptions {
5
+ /**
6
+ * Rewrite a call's arguments before they are forwarded to the remote API.
7
+ * Receives the endpoint key and the merged args, returns the args to send —
8
+ * e.g. to upload a local file referenced in the args and swap in its URL.
9
+ */
10
+ transformArgs?: (endpointKey: string, args: Record<string, unknown>) => Record<string, unknown> | Promise<Record<string, unknown>>;
11
+ }
12
+ /**
13
+ * Bind a contract to a remote HTTP client, producing a `ServiceDef` whose every
14
+ * handler forwards the call to the remote API. The transport twin of
15
+ * `implement`: instead of local business logic, each endpoint proxies to a
16
+ * deployed server.
17
+ *
18
+ * Used to build a thin local MCP / agent server that re-exposes a remote API —
19
+ * `buildMcpServer({ services: contracts.map((c) => implementRemote(c, http)) })`.
20
+ */
21
+ export declare function implementRemote<T extends Record<string, EndpointDef>>(contract: ContractDef<T, string>, http: HttpClient, options?: ImplementRemoteOptions): ServiceDef;
22
+ //# sourceMappingURL=remote.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/tools/remote.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAkB,MAAM,aAAa,CAAC;AAE5E,OAAO,KAAK,EAAa,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAc7D,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,aAAa,CAAC,EAAE,CACd,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EACnE,QAAQ,EAAE,WAAW,CAAC,CAAC,EAAE,MAAM,CAAC,EAChC,IAAI,EAAE,UAAU,EAChB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,UAAU,CAsCZ"}
@@ -0,0 +1,21 @@
1
+ import { z } from 'zod';
2
+ /** Keys of a Zod object schema's shape — `[]` for a non-object or absent schema. */
3
+ export declare function objectShapeKeys(schema?: z.ZodType): string[];
4
+ /**
5
+ * Merge an endpoint's `params` and `input` schemas into the single schema a
6
+ * tool advertises.
7
+ *
8
+ * - Both object → one merged `z.object`. A key declared in both is a contract
9
+ * bug and throws.
10
+ * - A non-object `input` (a union, a discriminated union, a refined / piped
11
+ * schema) is kept intact and intersected with `params` when present — the
12
+ * transport SDK converts it natively (a discriminated union becomes a clean
13
+ * `oneOf`).
14
+ *
15
+ * The merged schema is exactly the two source schemas side by side: nothing is
16
+ * coerced, flattened or transformed, so the schema a tool advertises is the
17
+ * schema its arguments are validated against (`executeToolMethod` parses each
18
+ * source schema over its own slice of the args).
19
+ */
20
+ export declare function mergeSchemas(paramsSchema?: z.ZodType<unknown>, inputSchema?: z.ZodType<unknown>): z.ZodType;
21
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/tools/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,GAAG,MAAM,EAAE,CAE5D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAC1B,YAAY,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EACjC,WAAW,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAC/B,CAAC,CAAC,OAAO,CA2BX"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Native MCP `view_file` tool — materialises a media URL/path into MCP content
3
+ * blocks so the model can SEE images and HEAR audio.
4
+ *
5
+ * Native (not contract-based): contract tools return JSON text via
6
+ * `formatMcpResult`; this returns multimodal content (`image` / `audio` blocks).
7
+ *
8
+ * `resolveMedia` is the extractable core — when other tools need inline media
9
+ * it becomes the shared resolver.
10
+ *
11
+ * Inputs are model-controlled, so this tool is guarded: URLs that resolve to a
12
+ * private/internal address are refused (SSRF), local file access is opt-in and
13
+ * sandboxed to a `baseDir`, and downloads are capped before they reach memory.
14
+ */
15
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
16
+ /** An MCP content block — text / image / audio (video cannot be inlined). */
17
+ export type McpMediaContent = {
18
+ type: 'text';
19
+ text: string;
20
+ } | {
21
+ type: 'image';
22
+ data: string;
23
+ mimeType: string;
24
+ } | {
25
+ type: 'audio';
26
+ data: string;
27
+ mimeType: string;
28
+ };
29
+ export interface ViewFileOptions {
30
+ /**
31
+ * Directory that local file paths must resolve within. Omitted → local file
32
+ * access is disabled and only `http(s)` URLs are accepted.
33
+ */
34
+ baseDir?: string;
35
+ /**
36
+ * Allow fetching URLs that resolve to private / loopback / link-local
37
+ * addresses. Default `false` — the SSRF guard. Enable only in a trusted
38
+ * network where the model is allowed to reach internal hosts.
39
+ */
40
+ allowPrivateHosts?: boolean;
41
+ }
42
+ /**
43
+ * Resolve a single media path/URL into MCP content. The extractable core —
44
+ * reusable wherever inline media is needed.
45
+ *
46
+ * - `image/*` / `audio/*` → an inline base64 block — the model sees / hears it;
47
+ * - `video/*` → a text link (MCP has no video block);
48
+ * - anything else, or too large → a text link.
49
+ */
50
+ export declare function resolveMedia(pathOrUrl: string, options?: ViewFileOptions): Promise<McpMediaContent[]>;
51
+ /**
52
+ * Register the native MCP `view_file` tool on a server. Pass to
53
+ * `createMcpHandler` via `nativeTools`. `options` controls the security
54
+ * boundary — see `ViewFileOptions`.
55
+ */
56
+ export declare function mountViewFile(server: McpServer, options?: ViewFileOptions): void;
57
+ //# sourceMappingURL=view-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"view-file.d.ts","sourceRoot":"","sources":["../../src/tools/view-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAMH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAsBzE,6EAA6E;AAC7E,MAAM,MAAM,eAAe,GACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACjD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtD,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AA6HD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,eAAe,EAAE,CAAC,CA+B5B;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,GAAE,eAAoB,GAAG,IAAI,CA4BpF"}
@@ -0,0 +1,12 @@
1
+ export { type AgentContext, type AgentMountConfig, mountAgent } from './tools/agent';
2
+ export { withJsonCoercion } from './tools/coerce';
3
+ export type { ToolCallHooks, ToolLifecycle, ToolResult } from './tools/execute';
4
+ export { flattenDiscriminatedUnion } from './tools/flatten';
5
+ export { buildToolManifest, type ToolManifestEntry } from './tools/manifest';
6
+ export { buildMcpServer, type IncompatibleSchemaPolicy, type McpMountConfig, type McpServerBuildConfig, mountMcp, validateMcpSchemas, } from './tools/mcp';
7
+ export { createMcpHandler, type McpHandlerConfig } from './tools/mcp-handler';
8
+ export { createStdioMcpServer, type StdioMcpServerConfig } from './tools/mcp-stdio';
9
+ export { collectTools, type MountableTool, type ToolExtend } from './tools/mount';
10
+ export { type ImplementRemoteOptions, implementRemote } from './tools/remote';
11
+ export { type McpMediaContent, mountViewFile, resolveMedia } from './tools/view-file';
12
+ //# sourceMappingURL=tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC7E,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,QAAQ,EACR,kBAAkB,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EAAE,oBAAoB,EAAE,KAAK,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAClF,OAAO,EAAE,KAAK,sBAAsB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAC9E,OAAO,EAAE,KAAK,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}