UNPKG

@langchain/mcp-adapters

Version:
1 lines 5.68 kB
{"version":3,"file":"hooks.cjs","names":["z"],"sources":["../src/hooks.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport type { Command } from \"@langchain/langgraph\";\nimport type { EmbeddedResource } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { ContentBlock } from \"@langchain/core/messages\";\nimport type { RunnableConfig } from \"@langchain/core/runnables\";\nimport type { ToolMessage } from \"@langchain/core/messages\";\n\n/**\n * state messages\n *\n * Note: this may not be defined in cases you don't use LangGraph or a LangGraph implementation like `createAgent`.\n * Also state can be defined arbitrarily by the user.\n */\nexport type State = Record<string, unknown>;\n\nconst toolCallRequestSchema = z.object({\n serverName: z.string(),\n name: z.string(),\n args: z.unknown(),\n});\nexport type ToolCallRequest = z.output<typeof toolCallRequestSchema>;\n\nconst toolResultBeforeSchema = z.tuple([\n z.custom<string | (ContentBlock | ContentBlock.Data.DataContentBlock)[]>(),\n z.array(\n z.union([\n z.custom<EmbeddedResource>(),\n z.custom<ContentBlock.Multimodal.Standard>(),\n ])\n ),\n]);\n\n/**\n * Tool result schema that users can return within the `afterToolCall` callback\n */\nconst toolResultSchema = z.union([\n /**\n * a basic string result\n */\n z.string(),\n /**\n * Command from LangGraph\n */\n z.custom<Command>(),\n /**\n * 2-tuple of content, artifact\n */\n toolResultBeforeSchema,\n /**\n * ToolMessage return\n */\n z.custom<ToolMessage>(),\n]);\nexport type ToolResult = z.output<typeof toolResultSchema>;\n\nconst toolCallResultSchema = z.object({\n ...toolCallRequestSchema.shape,\n result: toolResultBeforeSchema,\n});\n\nconst modifiedToolCallResultSchema = z.object({\n ...toolCallRequestSchema.shape,\n result: toolResultSchema,\n});\nexport type ModifiedToolCallResult = z.output<\n typeof modifiedToolCallResultSchema\n>;\n\nconst toolCallModificationSchema = z\n .object({\n headers: z.record(z.string()),\n args: z.unknown(),\n })\n .partial();\nexport type ToolCallModification = z.output<typeof toolCallModificationSchema>;\n\nexport const toolHooksSchema = z.object({\n /**\n * Called before a tool call is made.\n * Allows you to modify the tool call arguments or return a different tool call.\n *\n * @param toolCallRequest - The tool call request\n * @param toolCallRequest.name - The tool name\n * @param toolCallRequest.args - The tool call arguments\n * @param toolCallRequest.serverName - The server name\n * @param config - The runnable config\n * @returns The tool call modification\n *\n * @example\n * ```ts\n * const interceptor = {\n * beforeToolCall: (toolCallRequest, state, runtime) => {\n * return {\n * args: {\n * ...toolCallRequest.args,\n * custom: \"Custom Value\"\n * },\n * header: { \"X-Custom-Header\": \"Custom Value\" }\n * };\n * },\n * };\n * ```\n */\n beforeToolCall: z\n .function()\n .args(toolCallRequestSchema, z.custom<State>(), z.custom<RunnableConfig>())\n .returns(\n z.union([\n z.promise(toolCallModificationSchema),\n toolCallModificationSchema,\n z.void(),\n z.promise(z.void()),\n ])\n )\n .optional(),\n\n /**\n * Called after a tool call is made.\n * Allows you to modify the tool call result or return a different tool call result.\n *\n * @param toolCallResult - The tool call result\n * @param toolCallResult.args - The tool call arguments\n * @param toolCallResult.serverName - The server name\n * @param toolCallResult.name - The tool name\n * @param toolCallResult.result - The tool call result\n * @param config - The runnable config\n * @returns The tool call modification\n * @example\n * ```ts\n * const interceptor = {\n * afterToolCall: (toolCallResult, state, runtime) => {\n * if (toolCallResult.name === \"calculator\") {\n * return [\"Custom Value\", []];\n * }\n * return toolCallResult.result;\n * },\n * };\n * ```\n */\n afterToolCall: z\n .function()\n .args(toolCallResultSchema, z.custom<State>(), z.custom<RunnableConfig>())\n .returns(\n z.union([\n z.promise(modifiedToolCallResultSchema.pick({ result: true })),\n modifiedToolCallResultSchema.pick({ result: true }),\n z.void(),\n z.promise(z.void()),\n ])\n )\n .optional(),\n});\nexport type ToolHooks = z.input<typeof toolHooksSchema>;\n"],"mappings":";;;;AAeA,MAAM,wBAAwBA,SAAE,OAAO;CACrC,YAAYA,SAAE,QAAQ;CACtB,MAAMA,SAAE,QAAQ;CAChB,MAAMA,SAAE,SAAS;AAClB,EAAC;AAGF,MAAM,yB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