@lssm/lib.ai-agent 0.0.0-canary-20251217063201 → 0.0.0-canary-20251217073102

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.
@@ -1 +1,68 @@
1
- import{jsonSchemaToZodSafe as e}from"../schema/json-schema-to-zod.js";import*as t from"zod";import{McpServer as n}from"@modelcontextprotocol/sdk/server/mcp.js";function r(r,i){let a=new n({name:i.meta.name,version:`${i.meta.version}`});a.registerTool(i.meta.name,{description:i.description??`Interact with ${i.meta.name} agent`,inputSchema:t.object({message:t.string().describe(`The message or query to send to the agent`),sessionId:t.string().optional().describe(`Optional session ID to continue a conversation`)})},async e=>{let{message:t,sessionId:n}=e;return{content:[{type:`text`,text:(await r.generate({prompt:t,options:{sessionId:n}})).text}]}});for(let n of i.tools){let o=n.schema?e(n.schema):t.object({});a.registerTool(`${i.meta.name}.${n.name}`,{description:n.description??`Execute ${n.name} tool`,inputSchema:o},async e=>({content:[{type:`text`,text:(await r.generate({prompt:`Execute the ${n.name} tool with the following arguments: ${JSON.stringify(e)}`})).text}]}))}return a}function i(e){return r(e.agent,e.spec)}export{r as agentToMcpServer,i as createAgentMcpServer};
1
+ import { jsonSchemaToZodSafe } from "../schema/json-schema-to-zod.js";
2
+ import * as z$1 from "zod";
3
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
+
5
+ //#region src/tools/mcp-server.ts
6
+ /**
7
+ * Generate an MCP server that exposes a ContractSpec agent as a tool.
8
+ *
9
+ * This allows other AI agents (e.g., Claude Desktop, Cursor) to use
10
+ * your ContractSpec agents as tools, enabling agent-to-agent composition.
11
+ *
12
+ * @param agent - The ContractSpec agent to expose
13
+ * @param spec - The agent specification
14
+ * @returns MCP Server instance
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const server = agentToMcpServer(myAgent, myAgentSpec);
19
+ *
20
+ * // Run via stdio transport
21
+ * const transport = new StdioServerTransport();
22
+ * await server.connect(transport);
23
+ * ```
24
+ */
25
+ function agentToMcpServer(agent, spec) {
26
+ const server = new McpServer({
27
+ name: spec.meta.name,
28
+ version: `${spec.meta.version}`
29
+ });
30
+ server.registerTool(spec.meta.name, {
31
+ description: spec.description ?? `Interact with ${spec.meta.name} agent`,
32
+ inputSchema: z$1.object({
33
+ message: z$1.string().describe("The message or query to send to the agent"),
34
+ sessionId: z$1.string().optional().describe("Optional session ID to continue a conversation")
35
+ })
36
+ }, async (args) => {
37
+ const { message, sessionId } = args;
38
+ return { content: [{
39
+ type: "text",
40
+ text: (await agent.generate({
41
+ prompt: message,
42
+ options: { sessionId }
43
+ })).text
44
+ }] };
45
+ });
46
+ for (const toolConfig of spec.tools) {
47
+ const inputSchema = toolConfig.schema ? jsonSchemaToZodSafe(toolConfig.schema) : z$1.object({});
48
+ server.registerTool(`${spec.meta.name}.${toolConfig.name}`, {
49
+ description: toolConfig.description ?? `Execute ${toolConfig.name} tool`,
50
+ inputSchema
51
+ }, async (args) => {
52
+ return { content: [{
53
+ type: "text",
54
+ text: (await agent.generate({ prompt: `Execute the ${toolConfig.name} tool with the following arguments: ${JSON.stringify(args)}` })).text
55
+ }] };
56
+ });
57
+ }
58
+ return server;
59
+ }
60
+ /**
61
+ * Create an MCP server from configuration.
62
+ */
63
+ function createAgentMcpServer(config) {
64
+ return agentToMcpServer(config.agent, config.spec);
65
+ }
66
+
67
+ //#endregion
68
+ export { agentToMcpServer, createAgentMcpServer };
@@ -1 +1,79 @@
1
- import{jsonSchemaToZodSafe as e}from"../schema/json-schema-to-zod.js";import{tool as t}from"ai";function n(n,r,i={}){return t({description:n.description??n.name,inputSchema:e(n.schema),needsApproval:n.requiresApproval??!n.automationSafe,execute:async e=>{let t=await r(e,{agentId:i.agentId??`unknown`,sessionId:i.sessionId??`unknown`,tenantId:i.tenantId,actorId:i.actorId,metadata:i.metadata,signal:i.signal});return typeof t==`string`?t:JSON.stringify(t)}})}function r(e,t,r={}){let i={};for(let a of e){let e=t.get(a.name);if(!e)throw Error(`Missing handler for tool: ${a.name}`);i[a.name]=n(a,e,r)}return i}function i(e){return async(t,n)=>e(t,n)}function a(e){return new Map(Object.entries(e))}export{a as buildToolHandlers,i as createToolHandler,n as specToolToAISDKTool,r as specToolsToAISDKTools};
1
+ import { jsonSchemaToZodSafe } from "../schema/json-schema-to-zod.js";
2
+ import { tool } from "ai";
3
+
4
+ //#region src/tools/tool-adapter.ts
5
+ /**
6
+ * Convert ContractSpec AgentToolConfig to AI SDK CoreTool.
7
+ *
8
+ * @param specTool - The tool configuration from AgentSpec
9
+ * @param handler - The handler function for the tool
10
+ * @param context - Partial context to inject into handler calls
11
+ * @returns AI SDK CoreTool
12
+ */
13
+ function specToolToAISDKTool(specTool, handler, context = {}) {
14
+ return tool({
15
+ description: specTool.description ?? specTool.name,
16
+ inputSchema: jsonSchemaToZodSafe(specTool.schema),
17
+ needsApproval: specTool.requiresApproval ?? !specTool.automationSafe,
18
+ execute: async (input) => {
19
+ const result = await handler(input, {
20
+ agentId: context.agentId ?? "unknown",
21
+ sessionId: context.sessionId ?? "unknown",
22
+ tenantId: context.tenantId,
23
+ actorId: context.actorId,
24
+ metadata: context.metadata,
25
+ signal: context.signal
26
+ });
27
+ return typeof result === "string" ? result : JSON.stringify(result);
28
+ }
29
+ });
30
+ }
31
+ /**
32
+ * Convert multiple ContractSpec tool configs to AI SDK tools.
33
+ *
34
+ * @param specTools - Array of tool configurations
35
+ * @param handlers - Map of tool name to handler function
36
+ * @param context - Partial context to inject into handler calls
37
+ * @returns Record of AI SDK tools keyed by name
38
+ */
39
+ function specToolsToAISDKTools(specTools, handlers, context = {}) {
40
+ const tools = {};
41
+ for (const specTool of specTools) {
42
+ const handler = handlers.get(specTool.name);
43
+ if (!handler) throw new Error(`Missing handler for tool: ${specTool.name}`);
44
+ tools[specTool.name] = specToolToAISDKTool(specTool, handler, context);
45
+ }
46
+ return tools;
47
+ }
48
+ /**
49
+ * Type-safe tool handler builder.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const handler = createToolHandler<{ query: string }>((input, ctx) => {
54
+ * return `Searched for: ${input.query}`;
55
+ * });
56
+ * ```
57
+ */
58
+ function createToolHandler(handler) {
59
+ return async (input, context) => {
60
+ return handler(input, context);
61
+ };
62
+ }
63
+ /**
64
+ * Build a tool handlers map from an object.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const handlers = buildToolHandlers({
69
+ * search: async (input: { query: string }) => `Found: ${input.query}`,
70
+ * calculate: async (input: { a: number, b: number }) => `${input.a + input.b}`,
71
+ * });
72
+ * ```
73
+ */
74
+ function buildToolHandlers(handlersObj) {
75
+ return new Map(Object.entries(handlersObj));
76
+ }
77
+
78
+ //#endregion
79
+ export { buildToolHandlers, createToolHandler, specToolToAISDKTool, specToolsToAISDKTools };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lssm/lib.ai-agent",
3
- "version": "0.0.0-canary-20251217063201",
3
+ "version": "0.0.0-canary-20251217073102",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -25,17 +25,18 @@
25
25
  "dependencies": {
26
26
  "ai": "beta",
27
27
  "@ai-sdk/mcp": "beta",
28
+ "@ai-sdk/anthropic": "beta",
29
+ "@ai-sdk/google": "beta",
28
30
  "@ai-sdk/mistral": "beta",
29
31
  "@ai-sdk/openai": "beta",
30
- "@ai-sdk/anthropic": "beta",
31
32
  "@modelcontextprotocol/sdk": "^1.24.3",
32
- "@lssm/lib.contracts": "0.0.0-canary-20251217063201",
33
- "@lssm/lib.knowledge": "0.0.0-canary-20251217063201",
33
+ "@lssm/lib.contracts": "0.0.0-canary-20251217073102",
34
+ "@lssm/lib.knowledge": "0.0.0-canary-20251217073102",
34
35
  "zod": "^4.1.13"
35
36
  },
36
37
  "devDependencies": {
37
- "@lssm/tool.tsdown": "0.0.0-canary-20251217063201",
38
- "@lssm/tool.typescript": "0.0.0-canary-20251217063201",
38
+ "@lssm/tool.tsdown": "0.0.0-canary-20251217073102",
39
+ "@lssm/tool.typescript": "0.0.0-canary-20251217073102",
39
40
  "tsdown": "^0.17.4",
40
41
  "typescript": "^5.9.3"
41
42
  },