Skip to content

Commit 6dd75db

Browse files
authored
feat: fetch todos from cached AgentRun (#564)
* fetch todos implemented, using cached AgentRun to serve Todos list from
1 parent 652c756 commit 6dd75db

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

internal/mcpserver/agent/tool/tool.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ type CreateBranch struct {
5656
CommitMessage string
5757
BranchName string
5858
}
59+
60+
// GetAgentRunTodos is an MCP tool that gets the todos for a given agent run
61+
type GetAgentRunTodosTool struct {
62+
ConsoleTool
63+
}
64+
65+
// cachedAgentRun holds the current agent run fragment shared across tools
66+
var cachedAgentRun *client.AgentRunFragment

internal/mcpserver/agent/tool/updateanalysis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func (in *UpdateAnalysis) handler(ctx context.Context, request mcp.CallToolReque
4444
if err != nil {
4545
return mcp.NewToolResultError(fmt.Sprintf("failed to update analysis: %v", err)), nil
4646
}
47+
cachedAgentRun = &client.AgentRunFragment{
48+
Todos: agentRun.GetTodos(),
49+
}
4750

4851
return mcp.NewToolResultJSON(struct {
4952
Success bool `json:"success"`

internal/mcpserver/agent/tool/updatetodos.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (in *UpdateTodos) handler(ctx context.Context, request mcp.CallToolRequest)
3434
if err != nil {
3535
return mcp.NewToolResultError(fmt.Sprintf("failed to update todos: %v", err)), nil
3636
}
37+
cachedAgentRun = nil
3738

3839
return mcp.NewToolResultJSON(struct {
3940
Success bool `json:"success"`

0 commit comments

Comments
 (0)