11import { db } from '@sim/db'
2- import { account , mcpServers } from '@sim/db/schema'
2+ import { mcpServers } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { and , eq , inArray , isNull } from 'drizzle-orm'
55import { createMcpToolId } from '@/lib/mcp/utils'
6- import { refreshTokenIfNeeded , resolveOAuthAccountId } from '@/app/api/auth/oauth/utils'
76import { getAllBlocks } from '@/blocks'
87import type { BlockOutput } from '@/blocks/types'
98import {
@@ -30,6 +29,7 @@ import type { BlockHandler, ExecutionContext, StreamingExecution } from '@/execu
3029import { collectBlockData } from '@/executor/utils/block-data'
3130import { buildAPIUrl , buildAuthHeaders } from '@/executor/utils/http'
3231import { stringifyJSON } from '@/executor/utils/json'
32+ import { resolveVertexCredential } from '@/executor/utils/vertex-credential'
3333import { executeProviderRequest } from '@/providers'
3434import { getProviderFromModel , transformBlockTool } from '@/providers/utils'
3535import type { SerializedBlock } from '@/serializer/types'
@@ -439,24 +439,15 @@ export class AgentBlockHandler implements BlockHandler {
439439 tool : ToolInput
440440 ) : Promise < any > {
441441 const { serverId, toolName, serverName, ...userProvidedParams } = tool . params || { }
442-
443- const { filterSchemaForLLM } = await import ( '@/tools/params' )
444- const filteredSchema = filterSchemaForLLM (
445- tool . schema || { type : 'object' , properties : { } } ,
446- userProvidedParams
447- )
448-
449- const toolId = createMcpToolId ( serverId , toolName )
450-
451- return {
452- id : toolId ,
453- name : toolName ,
442+ return this . buildMcpTool ( {
443+ serverId,
444+ toolName,
454445 description :
455446 tool . schema ?. description || `MCP tool ${ toolName } from ${ serverName || serverId } ` ,
456- parameters : filteredSchema ,
457- params : userProvidedParams ,
458- usageControl : tool . usageControl || 'auto' ,
459- }
447+ schema : tool . schema || { type : 'object' , properties : { } } ,
448+ userProvidedParams,
449+ usageControl : tool . usageControl ,
450+ } )
460451 }
461452
462453 /**
@@ -585,22 +576,35 @@ export class AgentBlockHandler implements BlockHandler {
585576 serverId : string
586577 ) : Promise < any > {
587578 const { toolName, ...userProvidedParams } = tool . params || { }
579+ return this . buildMcpTool ( {
580+ serverId,
581+ toolName,
582+ description : mcpTool . description || `MCP tool ${ toolName } from ${ mcpTool . serverName } ` ,
583+ schema : mcpTool . inputSchema || { type : 'object' , properties : { } } ,
584+ userProvidedParams,
585+ usageControl : tool . usageControl ,
586+ } )
587+ }
588588
589+ private async buildMcpTool ( config : {
590+ serverId : string
591+ toolName : string
592+ description : string
593+ schema : any
594+ userProvidedParams : Record < string , any >
595+ usageControl ?: string
596+ } ) : Promise < any > {
589597 const { filterSchemaForLLM } = await import ( '@/tools/params' )
590- const filteredSchema = filterSchemaForLLM (
591- mcpTool . inputSchema || { type : 'object' , properties : { } } ,
592- userProvidedParams
593- )
594-
595- const toolId = createMcpToolId ( serverId , toolName )
598+ const filteredSchema = filterSchemaForLLM ( config . schema , config . userProvidedParams )
599+ const toolId = createMcpToolId ( config . serverId , config . toolName )
596600
597601 return {
598602 id : toolId ,
599- name : toolName ,
600- description : mcpTool . description || `MCP tool ${ toolName } from ${ mcpTool . serverName } ` ,
603+ name : config . toolName ,
604+ description : config . description ,
601605 parameters : filteredSchema ,
602- params : userProvidedParams ,
603- usageControl : tool . usageControl || 'auto' ,
606+ params : config . userProvidedParams ,
607+ usageControl : config . usageControl || 'auto' ,
604608 }
605609 }
606610
@@ -924,9 +928,9 @@ export class AgentBlockHandler implements BlockHandler {
924928 let finalApiKey : string | undefined = providerRequest . apiKey
925929
926930 if ( providerId === 'vertex' && providerRequest . vertexCredential ) {
927- finalApiKey = await this . resolveVertexCredential (
931+ finalApiKey = await resolveVertexCredential (
928932 providerRequest . vertexCredential ,
929- ctx . workflowId
933+ 'vertex-agent'
930934 )
931935 }
932936
@@ -973,37 +977,6 @@ export class AgentBlockHandler implements BlockHandler {
973977 }
974978 }
975979
976- /**
977- * Resolves a Vertex AI OAuth credential to an access token
978- */
979- private async resolveVertexCredential ( credentialId : string , workflowId : string ) : Promise < string > {
980- const requestId = `vertex-${ Date . now ( ) } `
981-
982- logger . info ( `[${ requestId } ] Resolving Vertex AI credential: ${ credentialId } ` )
983-
984- const resolved = await resolveOAuthAccountId ( credentialId )
985- if ( ! resolved ) {
986- throw new Error ( `Vertex AI credential is not a valid OAuth credential: ${ credentialId } ` )
987- }
988-
989- const credential = await db . query . account . findFirst ( {
990- where : eq ( account . id , resolved . accountId ) ,
991- } )
992-
993- if ( ! credential ) {
994- throw new Error ( `Vertex AI credential not found: ${ credentialId } ` )
995- }
996-
997- const { accessToken } = await refreshTokenIfNeeded ( requestId , credential , resolved . accountId )
998-
999- if ( ! accessToken ) {
1000- throw new Error ( 'Failed to get Vertex AI access token' )
1001- }
1002-
1003- logger . info ( `[${ requestId } ] Successfully resolved Vertex AI credential` )
1004- return accessToken
1005- }
1006-
1007980 private handleExecutionError (
1008981 error : any ,
1009982 startTime : number ,
@@ -1187,7 +1160,7 @@ export class AgentBlockHandler implements BlockHandler {
11871160 } ,
11881161 toolCalls : {
11891162 list : result . toolCalls ?. map ( this . formatToolCall . bind ( this ) ) || [ ] ,
1190- count : result . toolCalls ?. length || DEFAULTS . EXECUTION_TIME ,
1163+ count : result . toolCalls ?. length ?? 0 ,
11911164 } ,
11921165 providerTiming : result . timing ,
11931166 cost : result . cost ,
0 commit comments