|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/google/uuid" |
| 12 | + "github.com/kagent-dev/kagent/go/api/v1alpha2" |
| 13 | + "github.com/kagent-dev/kagent/go/internal/a2a" |
| 14 | + "github.com/kagent-dev/kagent/go/internal/version" |
| 15 | + "github.com/kagent-dev/kagent/go/pkg/auth" |
| 16 | + "github.com/mark3labs/mcp-go/mcp" |
| 17 | + mcpserver "github.com/mark3labs/mcp-go/server" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + ctrllog "sigs.k8s.io/controller-runtime/pkg/log" |
| 20 | + a2aclient "trpc.group/trpc-go/trpc-a2a-go/client" |
| 21 | + "trpc.group/trpc-go/trpc-a2a-go/protocol" |
| 22 | +) |
| 23 | + |
| 24 | +// MCPHandler handles MCP requests and bridges them to A2A endpoints |
| 25 | +type MCPHandler struct { |
| 26 | + kubeClient client.Client |
| 27 | + a2aBaseURL string |
| 28 | + authenticator auth.AuthProvider |
| 29 | + httpServer *mcpserver.StreamableHTTPServer |
| 30 | + lock sync.RWMutex |
| 31 | + // Map to store context IDs per session and agent |
| 32 | + contextBySessionAndAgent sync.Map |
| 33 | +} |
| 34 | + |
| 35 | +// NewMCPHandler creates a new MCP handler |
| 36 | +// Wraps the StreamableHTTPServer handler adds A2A bridging and context management. |
| 37 | +func NewMCPHandler(kubeClient client.Client, a2aBaseURL string, authenticator auth.AuthProvider) (*MCPHandler, error) { |
| 38 | + handler := &MCPHandler{ |
| 39 | + kubeClient: kubeClient, |
| 40 | + a2aBaseURL: a2aBaseURL, |
| 41 | + authenticator: authenticator, |
| 42 | + } |
| 43 | + |
| 44 | + // Create MCP server with tools and session cleanup hooks |
| 45 | + hooks := &mcpserver.Hooks{} |
| 46 | + hooks.AddOnUnregisterSession(func(ctx context.Context, session mcpserver.ClientSession) { |
| 47 | + sessionID := session.SessionID() |
| 48 | + handler.contextBySessionAndAgent.Range(func(key, _ any) bool { |
| 49 | + keyStr, ok := key.(string) |
| 50 | + if !ok { |
| 51 | + return true |
| 52 | + } |
| 53 | + if strings.HasPrefix(keyStr, sessionID+"|") { |
| 54 | + handler.contextBySessionAndAgent.Delete(key) |
| 55 | + } |
| 56 | + return true |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + s := mcpserver.NewMCPServer( |
| 61 | + "kagent-agents", |
| 62 | + version.Version, |
| 63 | + mcpserver.WithToolCapabilities(false), |
| 64 | + mcpserver.WithHooks(hooks), |
| 65 | + ) |
| 66 | + |
| 67 | + // Add list_agents tool |
| 68 | + s.AddTool(mcp.NewTool("list_agents", |
| 69 | + mcp.WithDescription("List invokable kagent agents (accepted + deploymentReady)"), |
| 70 | + ), handler.handleListAgents) |
| 71 | + |
| 72 | + // Add invoke_agent tool |
| 73 | + s.AddTool(mcp.NewTool("invoke_agent", |
| 74 | + mcp.WithDescription("Invoke a kagent agent via A2A"), |
| 75 | + mcp.WithString("agent", mcp.Description("Agent name (or namespace/name)"), mcp.Required()), |
| 76 | + mcp.WithString("task", mcp.Description("Task to run"), mcp.Required()), |
| 77 | + ), handler.handleInvokeAgent) |
| 78 | + |
| 79 | + // Create HTTP server |
| 80 | + handler.httpServer = mcpserver.NewStreamableHTTPServer(s) |
| 81 | + |
| 82 | + return handler, nil |
| 83 | +} |
| 84 | + |
| 85 | +// handleListAgents handles the list_agents MCP tool |
| 86 | +func (h *MCPHandler) handleListAgents(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 87 | + log := ctrllog.FromContext(ctx).WithName("mcp-handler").WithValues("tool", "list_agents") |
| 88 | + |
| 89 | + agentList := &v1alpha2.AgentList{} |
| 90 | + if err := h.kubeClient.List(ctx, agentList); err != nil { |
| 91 | + return mcp.NewToolResultErrorFromErr("list agents", err), nil |
| 92 | + } |
| 93 | + |
| 94 | + type agentSummary struct { |
| 95 | + Ref string `json:"ref"` |
| 96 | + Description string `json:"description,omitempty"` |
| 97 | + } |
| 98 | + |
| 99 | + agents := make([]agentSummary, 0) |
| 100 | + for _, agent := range agentList.Items { |
| 101 | + // Check if agent is accepted and deployment ready |
| 102 | + deploymentReady := false |
| 103 | + accepted := false |
| 104 | + for _, condition := range agent.Status.Conditions { |
| 105 | + if condition.Type == "Ready" && condition.Reason == "DeploymentReady" && condition.Status == "True" { |
| 106 | + deploymentReady = true |
| 107 | + } |
| 108 | + if condition.Type == "Accepted" && condition.Status == "True" { |
| 109 | + accepted = true |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if !accepted || !deploymentReady { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + ref := agent.Namespace + "/" + agent.Name |
| 118 | + description := agent.Spec.Description |
| 119 | + agents = append(agents, agentSummary{ |
| 120 | + Ref: ref, |
| 121 | + Description: description, |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + log.Info("Listed agents", "count", len(agents)) |
| 126 | + if len(agents) == 0 { |
| 127 | + return mcp.NewToolResultStructured(map[string]any{"agents": agents}, "No invokable agents found."), nil |
| 128 | + } |
| 129 | + |
| 130 | + var fallbackText strings.Builder |
| 131 | + for i, agent := range agents { |
| 132 | + if i > 0 { |
| 133 | + fallbackText.WriteByte('\n') |
| 134 | + } |
| 135 | + fallbackText.WriteString(agent.Ref) |
| 136 | + if agent.Description != "" { |
| 137 | + fallbackText.WriteString(" - ") |
| 138 | + fallbackText.WriteString(agent.Description) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return mcp.NewToolResultStructured(map[string]any{"agents": agents}, fallbackText.String()), nil |
| 143 | +} |
| 144 | + |
| 145 | +// handleInvokeAgent handles the invoke_agent MCP tool |
| 146 | +func (h *MCPHandler) handleInvokeAgent(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 147 | + log := ctrllog.FromContext(ctx).WithName("mcp-handler").WithValues("tool", "invoke_agent") |
| 148 | + |
| 149 | + agentRef, err := request.RequireString("agent") |
| 150 | + if err != nil { |
| 151 | + return mcp.NewToolResultError(err.Error()), nil |
| 152 | + } |
| 153 | + |
| 154 | + task, err := request.RequireString("task") |
| 155 | + if err != nil { |
| 156 | + return mcp.NewToolResultError(err.Error()), nil |
| 157 | + } |
| 158 | + |
| 159 | + // Parse agent reference (namespace/name or just name) |
| 160 | + agentNS, agentName, ok := strings.Cut(agentRef, "/") |
| 161 | + if !ok { |
| 162 | + return mcp.NewToolResultError("agent must be in format 'namespace/name'"), nil |
| 163 | + } |
| 164 | + agentRef = agentNS + "/" + agentName |
| 165 | + |
| 166 | + // Get session ID from context if available |
| 167 | + sessionID := "" |
| 168 | + if session := mcpserver.ClientSessionFromContext(ctx); session != nil { |
| 169 | + sessionID = session.SessionID() |
| 170 | + } else if headerSessionID := request.Header.Get(mcpserver.HeaderKeySessionID); headerSessionID != "" { |
| 171 | + sessionID = headerSessionID |
| 172 | + } |
| 173 | + if sessionID == "" { |
| 174 | + sessionID = uuid.New().String() |
| 175 | + } |
| 176 | + |
| 177 | + // Get or create context ID for this session and agent |
| 178 | + contextKey := sessionID + "|" + agentRef |
| 179 | + var contextIDPtr *string |
| 180 | + if prior, ok := h.contextBySessionAndAgent.Load(contextKey); ok { |
| 181 | + if priorStr, ok := prior.(string); ok && priorStr != "" { |
| 182 | + contextIDPtr = &priorStr |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Create A2A client |
| 187 | + a2aURL := fmt.Sprintf("%s/%s/", h.a2aBaseURL, agentRef) |
| 188 | + a2aClient, err := a2aclient.NewA2AClient(a2aURL, a2aclient.WithTimeout(30*time.Second)) |
| 189 | + if err != nil { |
| 190 | + log.Error(err, "Failed to create A2A client", "agent", agentRef) |
| 191 | + return mcp.NewToolResultErrorFromErr("a2a client", err), nil |
| 192 | + } |
| 193 | + |
| 194 | + // Send message via A2A |
| 195 | + result, err := a2aClient.SendMessage(ctx, protocol.SendMessageParams{ |
| 196 | + Message: protocol.Message{ |
| 197 | + Kind: protocol.KindMessage, |
| 198 | + Role: protocol.MessageRoleUser, |
| 199 | + ContextID: contextIDPtr, |
| 200 | + Parts: []protocol.Part{protocol.NewTextPart(task)}, |
| 201 | + }, |
| 202 | + }) |
| 203 | + if err != nil { |
| 204 | + log.Error(err, "Failed to send A2A message", "agent", agentRef) |
| 205 | + return mcp.NewToolResultErrorFromErr("a2a send", err), nil |
| 206 | + } |
| 207 | + |
| 208 | + // Extract response text and context ID |
| 209 | + var responseText, newContextID string |
| 210 | + switch a2aResult := result.Result.(type) { |
| 211 | + case *protocol.Message: |
| 212 | + responseText = a2a.ExtractText(*a2aResult) |
| 213 | + if a2aResult.ContextID != nil { |
| 214 | + newContextID = *a2aResult.ContextID |
| 215 | + } |
| 216 | + case *protocol.Task: |
| 217 | + newContextID = a2aResult.ContextID |
| 218 | + if a2aResult.Status.Message != nil { |
| 219 | + responseText = a2a.ExtractText(*a2aResult.Status.Message) |
| 220 | + } |
| 221 | + for _, artifact := range a2aResult.Artifacts { |
| 222 | + responseText += a2a.ExtractText(protocol.Message{Parts: artifact.Parts}) |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if responseText == "" { |
| 227 | + raw, err := result.MarshalJSON() |
| 228 | + if err != nil { |
| 229 | + return mcp.NewToolResultErrorFromErr("marshal result", err), nil |
| 230 | + } |
| 231 | + responseText = string(raw) |
| 232 | + } |
| 233 | + |
| 234 | + // Store new context ID if available |
| 235 | + if newContextID != "" { |
| 236 | + h.contextBySessionAndAgent.Store(contextKey, newContextID) |
| 237 | + } |
| 238 | + |
| 239 | + log.Info("Invoked agent", "agent", agentRef, "hasContextID", newContextID != "") |
| 240 | + return mcp.NewToolResultStructured(map[string]any{ |
| 241 | + "agent": agentRef, |
| 242 | + "text": responseText, |
| 243 | + }, responseText), nil |
| 244 | +} |
| 245 | + |
| 246 | +// ServeHTTP implements http.Handler interface |
| 247 | +func (h *MCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 248 | + // The MCP HTTP server handles all the routing internally |
| 249 | + h.httpServer.ServeHTTP(w, r) |
| 250 | +} |
| 251 | + |
| 252 | +// Shutdown gracefully shuts down the MCP handler |
| 253 | +func (h *MCPHandler) Shutdown(ctx context.Context) error { |
| 254 | + return h.httpServer.Shutdown(ctx) |
| 255 | +} |
0 commit comments