feat(tool): add remove error middleware & mcp toolcallresulthander (#147)
parent
d099dce571
commit
8c6efe243f
@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 CloudWeGo Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bytedance/sonic"
|
||||||
|
"github.com/cloudwego/eino-ext/components/tool/mcp/officialmcp"
|
||||||
|
"github.com/cloudwego/eino/components/tool"
|
||||||
|
"github.com/cloudwego/eino/compose"
|
||||||
|
"github.com/cloudwego/eino/schema"
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// main function demonstrates how to use the tool call result handler.
|
||||||
|
func main() {
|
||||||
|
// 1. Initialize context and get tools.
|
||||||
|
// The GetTools function is configured to use our custom toolCallResultHandler.
|
||||||
|
ctx := context.Background()
|
||||||
|
tools, err := GetTools(ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Create a new ToolNode.
|
||||||
|
// A ToolNode is a component that can execute tool calls.
|
||||||
|
tn, _ := compose.NewToolNode(ctx, &compose.ToolsNodeConfig{
|
||||||
|
Tools: tools,
|
||||||
|
})
|
||||||
|
|
||||||
|
// 3. Simulate a tool call message from an assistant.
|
||||||
|
// This message represents a request to call the 'web_search' tool.
|
||||||
|
msg := schema.AssistantMessage("call web_search tool", []schema.ToolCall{
|
||||||
|
{
|
||||||
|
ID: "1",
|
||||||
|
Function: schema.FunctionCall{
|
||||||
|
Name: "web_search",
|
||||||
|
Arguments: `{"url":"web_url"}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 4. Invoke the ToolNode.
|
||||||
|
// When tn.Invoke is called, it will execute the 'web_search' tool.
|
||||||
|
// After the tool returns a result, the toolCallResultHandler will be triggered
|
||||||
|
// to process the result before it is returned here.
|
||||||
|
result, err := tn.Invoke(ctx, msg)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = result
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type detailContent struct {
|
||||||
|
Summary string
|
||||||
|
Details string
|
||||||
|
}
|
||||||
|
|
||||||
|
const webSearchTool = "web_search"
|
||||||
|
|
||||||
|
// toolCallResultHandler is a callback function that gets executed after a tool call.
|
||||||
|
// It allows for the modification of the tool call's result before it's returned.
|
||||||
|
// This can be useful for tailoring the output, or in this case,
|
||||||
|
// condensing the result to save on token usage.
|
||||||
|
func toolCallResultHandler(ctx context.Context, name string, result *mcp.CallToolResult) (*mcp.CallToolResult, error) {
|
||||||
|
// First, check if the tool call resulted in an error.
|
||||||
|
if result.IsError {
|
||||||
|
marshaledResult, err := sonic.MarshalString(result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// If there was an error, return it to be handled upstream.
|
||||||
|
return nil, fmt.Errorf("failed to call official mcp tool, mcp server return error: %s", marshaledResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We're specifically interested in post-processing the 'web_search' tool's output.
|
||||||
|
if name == webSearchTool && len(result.Content) > 0 {
|
||||||
|
// The output format of the 'web_search' tool is known and consistent.
|
||||||
|
// It is expected to return a single content block, which is why we can safely access the first element.
|
||||||
|
content := result.Content[0]
|
||||||
|
// We also know that the content will be of type TextContent.
|
||||||
|
if textContent, ok := content.(*mcp.TextContent); ok {
|
||||||
|
detailCt := detailContent{}
|
||||||
|
// The Text field contains a JSON string with 'Summary' and 'Details'. We unmarshal it.
|
||||||
|
err := sonic.UnmarshalString(textContent.Text, &detailCt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// To reduce token consumption for the language model, if the 'Details' are too long (over 1000 chars),
|
||||||
|
// we replace the content with the shorter 'Summary'.
|
||||||
|
if len(detailCt.Details) > 1000 {
|
||||||
|
textContent.Text = detailCt.Summary
|
||||||
|
} else {
|
||||||
|
textContent.Text = detailCt.Details
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the result content with the potentially modified text.
|
||||||
|
result.Content[0] = textContent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the (possibly modified) result.
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTools initializes and returns a list of tools.
|
||||||
|
// It hooks in the toolCallResultHandler to process the results of any tool calls.
|
||||||
|
func GetTools(ctx context.Context) ([]tool.BaseTool, error) {
|
||||||
|
// officialmcp.GetTools is used to get the official MCP tools.
|
||||||
|
// We provide a custom configuration to it.
|
||||||
|
tools, err := officialmcp.GetTools(ctx, &officialmcp.Config{
|
||||||
|
// ToolCallResultHandler is a field in the config that takes a function.
|
||||||
|
// This function will be called with the result of every tool call.
|
||||||
|
ToolCallResultHandler: toolCallResultHandler,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tools, nil
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 CloudWeGo Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This example shows how to configure the errorremover middleware on a ToolsNode
|
||||||
|
// to catch errors during local tool invocation and return custom information.
|
||||||
|
// Run: go run ./components/tool/middlewares/errorremover/example
|
||||||
|
|
||||||
|
package errorremover
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cloudwego/eino/compose"
|
||||||
|
"github.com/cloudwego/eino/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// removeErrorHandler is an error handler function that generates a string
|
||||||
|
// describing the error that occurred during a tool call.
|
||||||
|
func removeErrorHandler(ctx context.Context, in *compose.ToolInput, err error) string {
|
||||||
|
// Formats the error message to include the tool name and the specific error content.
|
||||||
|
return fmt.Sprintf("Failed to call tool '%s', error message: '%s'", in.Name, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invokable creates a middleware endpoint for non-streaming (invokable) tools.
|
||||||
|
// It intercepts the tool's execution. If the tool returns an error, it calls the
|
||||||
|
// error handler and returns its result as a successful ToolOutput,
|
||||||
|
// effectively suppressing the original error.
|
||||||
|
func Invokable(next compose.InvokableToolEndpoint) compose.InvokableToolEndpoint {
|
||||||
|
return func(ctx context.Context, in *compose.ToolInput) (*compose.ToolOutput, error) {
|
||||||
|
// Proceed with the next middleware or the actual tool execution.
|
||||||
|
output, err := next(ctx, in)
|
||||||
|
// If an error occurs during execution.
|
||||||
|
if err != nil {
|
||||||
|
// Generate a custom error message using removeErrorHandler.
|
||||||
|
result := removeErrorHandler(ctx, in, err)
|
||||||
|
// Wrap the custom message in a successful ToolOutput and return it,
|
||||||
|
// with the error itself set to nil.
|
||||||
|
return &compose.ToolOutput{Result: result}, nil
|
||||||
|
}
|
||||||
|
// If there was no error, return the original output.
|
||||||
|
return output, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Streamable creates a middleware endpoint for streaming tools.
|
||||||
|
// It intercepts the tool's execution. If the tool returns an error, it calls the
|
||||||
|
// error handler and returns its result as a new stream containing a single successful item.
|
||||||
|
// This effectively replaces the error with a successful stream output.
|
||||||
|
func Streamable(next compose.StreamableToolEndpoint) compose.StreamableToolEndpoint {
|
||||||
|
return func(ctx context.Context, in *compose.ToolInput) (*compose.StreamToolOutput, error) {
|
||||||
|
// Proceed with the next middleware or the actual tool execution.
|
||||||
|
streamOutput, err := next(ctx, in)
|
||||||
|
// If an error occurs during execution.
|
||||||
|
if err != nil {
|
||||||
|
// Generate a custom error message using removeErrorHandler.
|
||||||
|
result := removeErrorHandler(ctx, in, err)
|
||||||
|
// Return the new stream as a successful output.
|
||||||
|
return &compose.StreamToolOutput{Result: schema.StreamReaderFromArray([]string{result})}, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
// If there was no error, return the original stream output.
|
||||||
|
return streamOutput, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware constructs and returns a compose.ToolMiddleware.
|
||||||
|
// This middleware is designed to catch errors from tool executions and replace them
|
||||||
|
// with a custom, successful output.
|
||||||
|
func Middleware() compose.ToolMiddleware {
|
||||||
|
return compose.ToolMiddleware{Invokable: Invokable, Streamable: Streamable}
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 CloudWeGo Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This example shows how to configure the jsonfix middleware on a ToolsNode
|
||||||
|
// to repair invalid JSON arguments before invoking a local tool.
|
||||||
|
// Run: go run ./components/tool/middlewares/jsonfix/example
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cloudwego/eino/components/tool"
|
||||||
|
"github.com/cloudwego/eino/components/tool/utils"
|
||||||
|
"github.com/cloudwego/eino/compose"
|
||||||
|
"github.com/cloudwego/eino/schema"
|
||||||
|
|
||||||
|
"github.com/cloudwego/eino-examples/components/tool/middlewares/errorremover"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WebSearch struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx := context.Background()
|
||||||
|
// 1. Create a mock "web_search" tool.
|
||||||
|
// This tool is designed to always return an error to demonstrate the middleware's functionality.
|
||||||
|
searcher, _ := utils.InferTool("web_search", "search content for web url", func(ctx context.Context, in *WebSearch) (string, error) {
|
||||||
|
// The tool call always fails.
|
||||||
|
return "", fmt.Errorf("not found web url")
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. Create a compose.ToolNode and inject the remove_error middleware.
|
||||||
|
// This middleware will intercept the tool execution lifecycle.
|
||||||
|
//
|
||||||
|
// IMPORTANT: Middleware order is critical. To catch errors from any subsequent
|
||||||
|
// middleware or from the tool itself, `remove_error.Middleware()` must be placed
|
||||||
|
// at the beginning of the `ToolCallMiddlewares` slice. Any middleware placed
|
||||||
|
// before it will not have its errors handled by this mechanism due to the
|
||||||
|
// sequential nature of middleware execution.
|
||||||
|
tn, _ := compose.NewToolNode(ctx, &compose.ToolsNodeConfig{
|
||||||
|
Tools: []tool.BaseTool{searcher},
|
||||||
|
ToolCallMiddlewares: []compose.ToolMiddleware{errorremover.Middleware()}, // Inject the remove_error middleware.
|
||||||
|
})
|
||||||
|
|
||||||
|
msg := schema.AssistantMessage("", []schema.ToolCall{
|
||||||
|
{
|
||||||
|
ID: "1",
|
||||||
|
Function: schema.FunctionCall{
|
||||||
|
Name: "web_search",
|
||||||
|
Arguments: `{"url":"web_url"}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 4. Simulate a tool call.
|
||||||
|
// Although the underlying 'web_search' tool fails, the 'Invoke' call will succeed.
|
||||||
|
// This is because the middleware catches the error and replaces the output with the result from the registered handler.
|
||||||
|
outs, err := tn.Invoke(ctx, msg)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Print the result.
|
||||||
|
// The output content will be the string returned by the error handler, not the original error message.
|
||||||
|
for _, o := range outs {
|
||||||
|
fmt.Println("tool:", o.ToolName, "id:", o.ToolCallID, "content:", o.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue