@@ -56,12 +56,12 @@ type MCPManager struct {
56
56
57
57
// MCPClient represents a connected MCP client with its configuration and tools.
58
58
type MCPClient struct {
59
- Name string // Unique name for this client
60
- Conn * client.Client // Active MCP client connection
61
- ExecutionConfig schemas.MCPClientConfig // Tool filtering settings
62
- ToolMap map [string ]schemas.Tool // Available tools mapped by name
63
- ConnectionInfo MCPClientConnectionInfo `json:"connection_info"` // Connection metadata for management
64
- cancelFunc context.CancelFunc `json:"-"` // Cancel function for SSE connections (not serialized)
59
+ Name string // Unique name for this client
60
+ Conn * client.Client // Active MCP client connection
61
+ ExecutionConfig schemas.MCPClientConfig // Tool filtering settings
62
+ ToolMap map [string ]schemas.ChatTool // Available tools mapped by name
63
+ ConnectionInfo MCPClientConnectionInfo `json:"connection_info"` // Connection metadata for management
64
+ cancelFunc context.CancelFunc `json:"-"` // Cancel function for SSE connections (not serialized)
65
65
}
66
66
67
67
// MCPClientConnectionInfo stores metadata about how a client is connected.
@@ -173,7 +173,7 @@ func (m *MCPManager) AddClient(config schemas.MCPClientConfig) error {
173
173
m .clientMap [config .Name ] = & MCPClient {
174
174
Name : config .Name ,
175
175
ExecutionConfig : config ,
176
- ToolMap : make (map [string ]schemas.Tool ),
176
+ ToolMap : make (map [string ]schemas.ChatTool ),
177
177
}
178
178
179
179
// Temporarily unlock for the connection attempt
@@ -228,7 +228,7 @@ func (m *MCPManager) removeClientUnsafe(name string) error {
228
228
}
229
229
230
230
// Clear client tool map
231
- client .ToolMap = make (map [string ]schemas.Tool )
231
+ client .ToolMap = make (map [string ]schemas.ChatTool )
232
232
233
233
delete (m .clientMap , name )
234
234
return nil
@@ -256,7 +256,7 @@ func (m *MCPManager) EditClientTools(name string, toolsToAdd []string, toolsToRe
256
256
client .ExecutionConfig = config
257
257
258
258
// Clear current tool map
259
- client .ToolMap = make (map [string ]schemas.Tool )
259
+ client .ToolMap = make (map [string ]schemas.ChatTool )
260
260
261
261
// Temporarily unlock for the network call
262
262
m .mu .Unlock ()
@@ -288,7 +288,7 @@ func (m *MCPManager) EditClientTools(name string, toolsToAdd []string, toolsToRe
288
288
289
289
// getAvailableTools returns all tools from connected MCP clients.
290
290
// Applies client filtering if specified in the context.
291
- func (m * MCPManager ) getAvailableTools (ctx context.Context ) []schemas.Tool {
291
+ func (m * MCPManager ) getAvailableTools (ctx context.Context ) []schemas.ChatTool {
292
292
m .mu .RLock ()
293
293
defer m .mu .RUnlock ()
294
294
@@ -303,7 +303,7 @@ func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.Tool {
303
303
excludeClients = existingExcludeClients
304
304
}
305
305
306
- tools := make ([]schemas.Tool , 0 )
306
+ tools := make ([]schemas.ChatTool , 0 )
307
307
for clientName , client := range m .clientMap {
308
308
// Apply client filtering logic
309
309
if ! m .shouldIncludeClient (clientName , includeClients , excludeClients ) {
@@ -348,7 +348,7 @@ func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.Tool {
348
348
// func(args EchoArgs) (string, error) {
349
349
// return args.Message, nil
350
350
// }, toolSchema)
351
- func (m * MCPManager ) registerTool (name , description string , handler MCPToolHandler [any ], toolSchema schemas.Tool ) error {
351
+ func (m * MCPManager ) registerTool (name , description string , handler MCPToolHandler [any ], toolSchema schemas.ChatTool ) error {
352
352
// Ensure local server is set up
353
353
if err := m .setupLocalHost (); err != nil {
354
354
return fmt .Errorf ("failed to setup local host: %w" , err )
@@ -453,7 +453,7 @@ func (m *MCPManager) createLocalMCPClient() (*MCPClient, error) {
453
453
ExecutionConfig : schemas.MCPClientConfig {
454
454
Name : BifrostMCPClientName ,
455
455
},
456
- ToolMap : make (map [string ]schemas.Tool ),
456
+ ToolMap : make (map [string ]schemas.ChatTool ),
457
457
ConnectionInfo : MCPClientConnectionInfo {
458
458
Type : schemas .MCPConnectionTypeInProcess , // Accurate: in-process (in-memory) transport
459
459
},
@@ -524,9 +524,9 @@ func (m *MCPManager) startLocalMCPServer() error {
524
524
// - toolCall: The tool call to execute (from assistant message)
525
525
//
526
526
// Returns:
527
- // - schemas.BifrostMessage : Tool message with execution result
527
+ // - schemas.ChatMessage : Tool message with execution result
528
528
// - error: Any execution error
529
- func (m * MCPManager ) executeTool (ctx context.Context , toolCall schemas.ToolCall ) (* schemas.BifrostMessage , error ) {
529
+ func (m * MCPManager ) executeTool (ctx context.Context , toolCall schemas.ChatAssistantMessageToolCall ) (* schemas.ChatMessage , error ) {
530
530
if toolCall .Function .Name == nil {
531
531
return nil , fmt .Errorf ("tool call missing function name" )
532
532
}
@@ -600,7 +600,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
600
600
m .clientMap [config .Name ] = & MCPClient {
601
601
Name : config .Name ,
602
602
ExecutionConfig : config ,
603
- ToolMap : make (map [string ]schemas.Tool ),
603
+ ToolMap : make (map [string ]schemas.ChatTool ),
604
604
ConnectionInfo : MCPClientConnectionInfo {
605
605
Type : config .ConnectionType ,
606
606
},
@@ -679,7 +679,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
679
679
if err != nil {
680
680
m .logger .Warn (fmt .Sprintf ("%s Failed to retrieve tools from %s: %v" , MCPLogPrefix , config .Name , err ))
681
681
// Continue with connection even if tool retrieval fails
682
- tools = make (map [string ]schemas.Tool )
682
+ tools = make (map [string ]schemas.ChatTool )
683
683
}
684
684
685
685
// Second lock: Update client with final connection details and tools
@@ -711,7 +711,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
711
711
}
712
712
713
713
// retrieveExternalTools retrieves and filters tools from an external MCP server without holding locks.
714
- func (m * MCPManager ) retrieveExternalTools (ctx context.Context , client * client.Client , config schemas.MCPClientConfig ) (map [string ]schemas.Tool , error ) {
714
+ func (m * MCPManager ) retrieveExternalTools (ctx context.Context , client * client.Client , config schemas.MCPClientConfig ) (map [string ]schemas.ChatTool , error ) {
715
715
// Get available tools from external server
716
716
listRequest := mcp.ListToolsRequest {
717
717
PaginatedRequest : mcp.PaginatedRequest {
@@ -727,10 +727,10 @@ func (m *MCPManager) retrieveExternalTools(ctx context.Context, client *client.C
727
727
}
728
728
729
729
if toolsResponse == nil {
730
- return make (map [string ]schemas.Tool ), nil // No tools available
730
+ return make (map [string ]schemas.ChatTool ), nil // No tools available
731
731
}
732
732
733
- tools := make (map [string ]schemas.Tool )
733
+ tools := make (map [string ]schemas.ChatTool )
734
734
735
735
// toolsResponse is already a ListToolsResult
736
736
for _ , mcpTool := range toolsResponse .Tools {
@@ -796,13 +796,13 @@ func (m *MCPManager) shouldSkipToolForRequest(toolName string, ctx context.Conte
796
796
}
797
797
798
798
// convertMCPToolToBifrostSchema converts an MCP tool definition to Bifrost format.
799
- func (m * MCPManager ) convertMCPToolToBifrostSchema (mcpTool * mcp.Tool ) schemas.Tool {
800
- return schemas.Tool {
801
- Type : "function" ,
802
- Function : schemas.Function {
799
+ func (m * MCPManager ) convertMCPToolToBifrostSchema (mcpTool * mcp.Tool ) schemas.ChatTool {
800
+ return schemas.ChatTool {
801
+ Type : schemas . ChatToolTypeFunction ,
802
+ Function : & schemas.ChatToolFunction {
803
803
Name : mcpTool .Name ,
804
- Description : mcpTool .Description ,
805
- Parameters : schemas.FunctionParameters {
804
+ Description : Ptr ( mcpTool .Description ) ,
805
+ Parameters : & schemas.ToolFunctionParameters {
806
806
Type : mcpTool .InputSchema .Type ,
807
807
Properties : mcpTool .InputSchema .Properties ,
808
808
Required : mcpTool .InputSchema .Required ,
@@ -852,13 +852,13 @@ func (m *MCPManager) extractTextFromMCPResponse(toolResponse *mcp.CallToolResult
852
852
}
853
853
854
854
// createToolResponseMessage creates a tool response message with the execution result.
855
- func (m * MCPManager ) createToolResponseMessage (toolCall schemas.ToolCall , responseText string ) * schemas.BifrostMessage {
856
- return & schemas.BifrostMessage {
857
- Role : schemas .ModelChatMessageRoleTool ,
858
- Content : schemas.MessageContent {
855
+ func (m * MCPManager ) createToolResponseMessage (toolCall schemas.ChatAssistantMessageToolCall , responseText string ) * schemas.ChatMessage {
856
+ return & schemas.ChatMessage {
857
+ Role : schemas .ChatMessageRoleTool ,
858
+ Content : schemas.ChatMessageContent {
859
859
ContentStr : & responseText ,
860
860
},
861
- ToolMessage : & schemas.ToolMessage {
861
+ ChatToolMessage : & schemas.ChatToolMessage {
862
862
ToolCallID : toolCall .ID ,
863
863
},
864
864
}
@@ -867,31 +867,51 @@ func (m *MCPManager) createToolResponseMessage(toolCall schemas.ToolCall, respon
867
867
func (m * MCPManager ) addMCPToolsToBifrostRequest (ctx context.Context , req * schemas.BifrostRequest ) * schemas.BifrostRequest {
868
868
mcpTools := m .getAvailableTools (ctx )
869
869
if len (mcpTools ) > 0 {
870
- // Initialize tools array if needed
871
- if req .Params == nil {
872
- req .Params = & schemas.ModelParameters {}
873
- }
874
- if req .Params .Tools == nil {
875
- req .Params .Tools = & []schemas.Tool {}
876
- }
877
- tools := * req .Params .Tools
870
+ switch req .RequestType {
871
+ case schemas .ChatCompletionRequest , schemas .ChatCompletionStreamRequest :
872
+ req .ChatRequest .Params = & schemas.ChatParameters {}
878
873
879
- // Create a map of existing tool names for O(1) lookup
880
- existingToolsMap := make (map [string ]bool )
881
- for _ , tool := range tools {
882
- existingToolsMap [tool .Function .Name ] = true
883
- }
874
+ tools := req .ChatRequest .Params .Tools
884
875
885
- // Add MCP tools that are not already present
886
- for _ , mcpTool := range mcpTools {
887
- if ! existingToolsMap [mcpTool .Function .Name ] {
888
- tools = append (tools , mcpTool )
889
- // Update the map to prevent duplicates within MCP tools as well
890
- existingToolsMap [mcpTool .Function .Name ] = true
876
+ // Create a map of existing tool names for O(1) lookup
877
+ existingToolsMap := make (map [string ]bool )
878
+ for _ , tool := range tools {
879
+ existingToolsMap [tool .Function .Name ] = true
891
880
}
892
- }
893
- req .Params .Tools = & tools
894
881
882
+ // Add MCP tools that are not already present
883
+ for _ , mcpTool := range mcpTools {
884
+ if ! existingToolsMap [mcpTool .Function .Name ] {
885
+ tools = append (tools , mcpTool )
886
+ // Update the map to prevent duplicates within MCP tools as well
887
+ existingToolsMap [mcpTool .Function .Name ] = true
888
+ }
889
+ }
890
+ req .ChatRequest .Params .Tools = tools
891
+ case schemas .ResponsesRequest , schemas .ResponsesStreamRequest :
892
+ req .ResponsesRequest .Params = & schemas.ResponsesParameters {}
893
+
894
+ tools := req .ResponsesRequest .Params .Tools
895
+
896
+ // Create a map of existing tool names for O(1) lookup
897
+ existingToolsMap := make (map [string ]bool )
898
+ for _ , tool := range tools {
899
+ if tool .Name != nil {
900
+ existingToolsMap [* tool .Name ] = true
901
+ }
902
+ }
903
+
904
+ // Add MCP tools that are not already present
905
+ for _ , mcpTool := range mcpTools {
906
+ if ! existingToolsMap [mcpTool .Function .Name ] {
907
+ responsesTool := mcpTool .ToResponsesTool ()
908
+ tools = append (tools , * responsesTool )
909
+ // Update the map to prevent duplicates within MCP tools as well
910
+ existingToolsMap [* responsesTool .Name ] = true
911
+ }
912
+ }
913
+ req .ResponsesRequest .Params .Tools = tools
914
+ }
895
915
}
896
916
return req
897
917
}
0 commit comments