1- import { MCPClientManager } from "agents/mcp/client"
2- import { LanguageModelV1 , ToolSet , streamText , tool } from "ai"
3- import { jsonSchemaToZod , type JsonSchemaObject } from "@n8n/json-schema-to-zod" ;
1+ import { jsonSchemaToZod } from '@n8n/json-schema-to-zod'
2+ import { MCPClientManager } from 'agents/mcp/client'
3+ import { LanguageModelV1 , streamText , tool , ToolSet } from 'ai'
44
5- export async function runTask ( model : LanguageModelV1 , input : string ) {
6- const clientManager = new MCPClientManager ( "test-client" , "0.0.0" )
7- await clientManager . connect ( "http://localhost:8787/sse" )
5+ import type { JsonSchemaObject } from '@n8n/json-schema-to-zod'
86
9- const tools = clientManager . listTools ( )
10- const toolSet : ToolSet = tools . reduce ( ( acc , v ) => {
11- acc [ v . name ] = tool ( {
12- parameters : jsonSchemaToZod ( v . inputSchema as JsonSchemaObject ) ,
13- description : v . description ,
14- execute : async ( args , opts ) => {
15- const res = await clientManager . callTool ( v , args , { signal : opts . abortSignal } )
16- console . log ( res . toolResult )
17- return res . content
18- } ,
19- } )
20- return acc
21- } , { } as ToolSet )
7+ export async function runTask ( model : LanguageModelV1 , input : string ) {
8+ const clientManager = new MCPClientManager ( 'test-client' , '0.0.0' )
9+ await clientManager . connect ( 'http://localhost:8787/sse' )
2210
23- const res = streamText ( {
24- model,
25- system : "You are an assistant responsible for evaluating the results of calling various tools. Given the user's query, use the tools available to you to answer the question." ,
26- tools : toolSet ,
27- prompt : input ,
28- maxRetries : 1 ,
29- maxSteps : 10 ,
30- } )
11+ const tools = clientManager . listTools ( )
12+ const toolSet : ToolSet = tools . reduce ( ( acc , v ) => {
13+ acc [ v . name ] = tool ( {
14+ parameters : jsonSchemaToZod ( v . inputSchema as JsonSchemaObject ) ,
15+ description : v . description ,
16+ execute : async ( args , opts ) => {
17+ const res = await clientManager . callTool ( v , args , { signal : opts . abortSignal } )
18+ console . log ( res . toolResult )
19+ return res . content
20+ } ,
21+ } )
22+ return acc
23+ } , { } as ToolSet )
3124
32- for await ( const part of res . fullStream ) {
25+ const res = streamText ( {
26+ model,
27+ system :
28+ "You are an assistant responsible for evaluating the results of calling various tools. Given the user's query, use the tools available to you to answer the question." ,
29+ tools : toolSet ,
30+ prompt : input ,
31+ maxRetries : 1 ,
32+ maxSteps : 10 ,
33+ } )
3334
34- }
35+ for await ( const part of res . fullStream ) {
36+ }
3537
36- // convert into an LLM readable result so our factuality checker can validate tool calls
37- let messagesWithTools = ""
38- const messages = ( await res . response ) . messages
39- for ( const message of messages ) {
40- console . log ( message . content )
41- for ( const messagePart of message . content ) {
42- if ( typeof messagePart === " string" ) {
43- messagesWithTools += `<message_content type="text">${ messagePart } </message_content>`
44- } else if ( messagePart . type === " tool-call" ) {
45- messagesWithTools += `<message_content type=${ messagePart . type } >
38+ // convert into an LLM readable result so our factuality checker can validate tool calls
39+ let messagesWithTools = ''
40+ const messages = ( await res . response ) . messages
41+ for ( const message of messages ) {
42+ console . log ( message . content )
43+ for ( const messagePart of message . content ) {
44+ if ( typeof messagePart === ' string' ) {
45+ messagesWithTools += `<message_content type="text">${ messagePart } </message_content>`
46+ } else if ( messagePart . type === ' tool-call' ) {
47+ messagesWithTools += `<message_content type=${ messagePart . type } >
4648 <tool_name>${ messagePart . toolName } </tool_name>
4749 <tool_arguments>${ JSON . stringify ( messagePart . args ) } </tool_arguments>
4850</message_content>`
49- } else if ( messagePart . type === " text" ) {
50- messagesWithTools += `<message_content type=${ messagePart . type } >${ messagePart . text } </message_content>`
51- }
52- }
53- }
51+ } else if ( messagePart . type === ' text' ) {
52+ messagesWithTools += `<message_content type=${ messagePart . type } >${ messagePart . text } </message_content>`
53+ }
54+ }
55+ }
5456
55- return messagesWithTools
56- }
57+ return messagesWithTools
58+ }
0 commit comments