|
| 1 | +package tool |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + "github.com/mark3labs/mcp-go/server" |
| 9 | +) |
| 10 | + |
| 11 | +func (in *GetAgentRunTodosTool) Install(server *server.MCPServer) { |
| 12 | + server.AddTool( |
| 13 | + mcp.NewTool( |
| 14 | + in.name, |
| 15 | + mcp.WithDescription(in.description), |
| 16 | + ), |
| 17 | + in.handler, |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +func (in *GetAgentRunTodosTool) handler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 22 | + runId, err := request.RequireString("runId") |
| 23 | + if err != nil { |
| 24 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get runId: %v", err)), nil |
| 25 | + } |
| 26 | + |
| 27 | + // if the agentRun is not in the cache, fetch it from the API and cache it |
| 28 | + if cachedAgentRun == nil { |
| 29 | + fragment, err := in.client.GetAgentRun(ctx, runId) |
| 30 | + if err != nil { |
| 31 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get agent run: %v", err)), nil |
| 32 | + } |
| 33 | + cachedAgentRun = fragment |
| 34 | + } |
| 35 | + |
| 36 | + // we need to convert the todos in order to return the correct format for the MCP server |
| 37 | + todos := make([]map[string]interface{}, 0) |
| 38 | + if cachedAgentRun != nil && cachedAgentRun.Todos != nil { |
| 39 | + for _, todo := range cachedAgentRun.Todos { |
| 40 | + todoMap := map[string]interface{}{ |
| 41 | + "title": todo.Title, |
| 42 | + "done": todo.Done, |
| 43 | + "description": todo.Description, |
| 44 | + } |
| 45 | + todos = append(todos, todoMap) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return mcp.NewToolResultJSON(struct { |
| 50 | + Success bool `json:"success"` |
| 51 | + Message string `json:"message"` |
| 52 | + RunId string `json:"runId"` |
| 53 | + Todos []map[string]interface{} `json:"todos"` |
| 54 | + Source string `json:"source"` |
| 55 | + }{ |
| 56 | + Success: true, |
| 57 | + Message: fmt.Sprintf("Successfully fetched todos for agent run %s", runId), |
| 58 | + RunId: runId, |
| 59 | + Todos: todos, |
| 60 | + Source: "cache", |
| 61 | + }) |
| 62 | +} |
0 commit comments