xonecas.io
60255
Zn

Zoea-Nova / commit 28856fc

feat(mcp): add caller context to tool handlers

Enable tools to identify which mysis is calling them:
- Add CallerContext struct with MysisID and MysisName
- Add ToolHandlerWithContext type for context-aware handlers
- Add RegisterToolWithContext() method to Proxy
- Update CallTool() signature to accept CallerContext
- Update zoea_broadcast to use BroadcastFrom with caller.MysisID
- Update Mysis.executeToolCall() to pass caller context
- Update all CallTool call sites with CallerContext parameter
- Add BroadcastFrom() to Orchestrator interface and adapters

xonecas2026-02-05 28856fc7995e7dfa58ca1cf33da712ecf160f89a parent ff99e9e

4 files changed

cmd/zoea/main.go+10−2
@@ -275,6 +275,10 @@ func (a *commanderAdapter) BroadcastAsync(message string) error {
275275 return a.commander.BroadcastAsync(message)
276276 }
277277
278+func (a *commanderAdapter) BroadcastFrom(senderID, message string) error {
279+ return a.commander.BroadcastFrom(senderID, message)
280+}
281+
278282 func (a *commanderAdapter) SearchMessages(mysisID, query string, limit int) ([]mcp.SearchResult, error) {
279283 memories, err := a.commander.Store().SearchMemories(mysisID, query, limit)
280284 if err != nil {
@@ -406,7 +410,7 @@ func runMCPTest(configPath string) {
406410 // Test calling a local tool
407411 fmt.Println("\n--- Testing Local Tool Call ---")
408412 fmt.Println("Calling: zoea_swarm_status")
409- result, err := mcpProxy.CallTool(ctx, "zoea_swarm_status", nil)
413+ result, err := mcpProxy.CallTool(ctx, mcp.CallerContext{}, "zoea_swarm_status", nil)
410414 if err != nil {
411415 fmt.Printf("ERROR: %v\n", err)
412416 } else if result.IsError {
@@ -428,7 +432,7 @@ func runMCPTest(configPath string) {
428432 }
429433 if upstreamTool != nil {
430434 fmt.Printf("Calling: %s\n", upstreamTool.Name)
431- result, err := mcpProxy.CallTool(ctx, upstreamTool.Name, nil)
435+ result, err := mcpProxy.CallTool(ctx, mcp.CallerContext{}, upstreamTool.Name, nil)
432436 if err != nil {
433437 fmt.Printf("ERROR: %v\n", err)
434438 } else if result.IsError {
@@ -480,6 +484,10 @@ func (m *mockOrchestrator) BroadcastAsync(message string) error {
480484 return fmt.Errorf("not available in test mode")
481485 }
482486
487+func (m *mockOrchestrator) BroadcastFrom(senderID, message string) error {
488+ return fmt.Errorf("not available in test mode")
489+}
490+
483491 func (m *mockOrchestrator) SearchMessages(mysisID, query string, limit int) ([]mcp.SearchResult, error) {
484492 return []mcp.SearchResult{}, nil
485493 }
internal/mcp/mcp_test.go+9−5
@@ -82,7 +82,7 @@ func TestProxyLocalTools(t *testing.T) {
8282 }
8383
8484 // Call tool
85- result, err := proxy.CallTool(ctx, "test_tool", nil)
85+ result, err := proxy.CallTool(ctx, CallerContext{}, "test_tool", nil)
8686 if err != nil {
8787 t.Fatalf("CallTool() error: %v", err)
8888 }
@@ -98,7 +98,7 @@ func TestProxyToolNotFound(t *testing.T) {
9898 proxy := NewProxy("") // No upstream
9999
100100 ctx := context.Background()
101- result, err := proxy.CallTool(ctx, "nonexistent", nil)
101+ result, err := proxy.CallTool(ctx, CallerContext{}, "nonexistent", nil)
102102 if err != nil {
103103 t.Fatalf("CallTool() error: %v", err)
104104 }
@@ -221,6 +221,10 @@ func (m *mockOrchestrator) BroadcastAsync(message string) error {
221221 return nil
222222 }
223223
224+func (m *mockOrchestrator) BroadcastFrom(senderID, message string) error {
225+ return nil
226+}
227+
224228 func (m *mockOrchestrator) SearchMessages(mysisID, query string, limit int) ([]SearchResult, error) {
225229 return []SearchResult{}, nil
226230 }
@@ -256,7 +260,7 @@ func TestOrchestratorTools(t *testing.T) {
256260 ctx := context.Background()
257261
258262 // Test zoea_swarm_status
259- result, err := proxy.CallTool(ctx, "zoea_swarm_status", nil)
263+ result, err := proxy.CallTool(ctx, CallerContext{}, "zoea_swarm_status", nil)
260264 if err != nil {
261265 t.Fatalf("CallTool(zoea_swarm_status) error: %v", err)
262266 }
@@ -265,7 +269,7 @@ func TestOrchestratorTools(t *testing.T) {
265269 }
266270
267271 // Test zoea_list_myses
268- result, err = proxy.CallTool(ctx, "zoea_list_myses", nil)
272+ result, err = proxy.CallTool(ctx, CallerContext{}, "zoea_list_myses", nil)
269273 if err != nil {
270274 t.Fatalf("CallTool(zoea_list_myses) error: %v", err)
271275 }
@@ -291,7 +295,7 @@ func TestZoeaListMysesPayloadMinimal(t *testing.T) {
291295 ctx := context.Background()
292296
293297 // Call zoea_list_myses
294- result, err := proxy.CallTool(ctx, "zoea_list_myses", nil)
298+ result, err := proxy.CallTool(ctx, CallerContext{}, "zoea_list_myses", nil)
295299 if err != nil {
296300 t.Fatalf("CallTool(zoea_list_myses) error: %v", err)
297301 }
internal/mcp/proxy.go+34−9
@@ -10,6 +10,15 @@ import (
1010 // ToolHandler is a function that handles a tool call.
1111 type ToolHandler func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error)
1212
13+// CallerContext provides information about who is calling a tool.
14+type CallerContext struct {
15+ MysisID string
16+ MysisName string
17+}
18+
19+// ToolHandlerWithContext is a function that handles a tool call with caller context.
20+type ToolHandlerWithContext func(ctx context.Context, caller CallerContext, arguments json.RawMessage) (*ToolResult, error)
21+
1322 type AccountStore interface {
1423 CreateAccount(username, password string) (*Account, error)
1524 MarkAccountInUse(username string) error
@@ -24,11 +33,12 @@ type Account struct {
2433
2534 // Proxy combines an upstream MCP client with local tool handlers.
2635 type Proxy struct {
27- mu sync.RWMutex
28- upstream *Client
29- localTools map[string]Tool
30- localHandlers map[string]ToolHandler
31- accountStore AccountStore
36+ mu sync.RWMutex
37+ upstream *Client
38+ localTools map[string]Tool
39+ localHandlers map[string]ToolHandler
40+ contextHandlers map[string]ToolHandlerWithContext
41+ accountStore AccountStore
3242 }
3343
3444 // NewProxy creates a new MCP proxy.
@@ -39,9 +49,10 @@ func NewProxy(upstreamEndpoint string) *Proxy {
3949 }
4050
4151 return &Proxy{
42- upstream: upstream,
43- localTools: make(map[string]Tool),
44- localHandlers: make(map[string]ToolHandler),
52+ upstream: upstream,
53+ localTools: make(map[string]Tool),
54+ localHandlers: make(map[string]ToolHandler),
55+ contextHandlers: make(map[string]ToolHandlerWithContext),
4556 }
4657 }
4758
@@ -60,6 +71,15 @@ func (p *Proxy) RegisterTool(tool Tool, handler ToolHandler) {
6071 p.localHandlers[tool.Name] = handler
6172 }
6273
74+// RegisterToolWithContext registers a tool handler that receives caller context.
75+func (p *Proxy) RegisterToolWithContext(tool Tool, handler ToolHandlerWithContext) {
76+ p.mu.Lock()
77+ defer p.mu.Unlock()
78+
79+ p.localTools[tool.Name] = tool
80+ p.contextHandlers[tool.Name] = handler
81+}
82+
6383 // ListTools returns all available tools (local + upstream).
6484 func (p *Proxy) ListTools(ctx context.Context) ([]Tool, error) {
6585 p.mu.RLock()
@@ -87,12 +107,17 @@ func (p *Proxy) ListTools(ctx context.Context) ([]Tool, error) {
87107 }
88108
89109 // CallTool invokes a tool, checking local handlers first then upstream.
90-func (p *Proxy) CallTool(ctx context.Context, name string, arguments json.RawMessage) (*ToolResult, error) {
110+func (p *Proxy) CallTool(ctx context.Context, caller CallerContext, name string, arguments json.RawMessage) (*ToolResult, error) {
91111 p.mu.RLock()
92112 handler, isLocal := p.localHandlers[name]
113+ contextHandler, hasContext := p.contextHandlers[name]
93114 accountStore := p.accountStore
94115 p.mu.RUnlock()
95116
117+ if hasContext {
118+ return contextHandler(ctx, caller, arguments)
119+ }
120+
96121 // Try local handler first
97122 if isLocal {
98123 return handler(ctx, arguments)
internal/mcp/tools.go+6−5
@@ -50,6 +50,7 @@ type Orchestrator interface {
5050 GetStateCounts() map[string]int
5151 SendMessageAsync(mysisID, message string) error
5252 BroadcastAsync(message string) error
53+ BroadcastFrom(senderID, message string) error
5354 SearchMessages(mysisID, query string, limit int) ([]SearchResult, error)
5455 SearchReasoning(mysisID, query string, limit int) ([]ReasoningResult, error)
5556 SearchBroadcasts(query string, limit int) ([]BroadcastResult, error)
@@ -145,10 +146,10 @@ func RegisterOrchestratorTools(proxy *Proxy, orchestrator Orchestrator) {
145146 )
146147
147148 // Broadcast message tool
148- proxy.RegisterTool(
149+ proxy.RegisterToolWithContext(
149150 Tool{
150151 Name: "zoea_broadcast",
151- Description: "Send a message to all running myses",
152+ Description: "Send a message to all running myses (you will not receive your own broadcast)",
152153 InputSchema: json.RawMessage(`{
153154 "type": "object",
154155 "properties": {
@@ -157,7 +158,7 @@ func RegisterOrchestratorTools(proxy *Proxy, orchestrator Orchestrator) {
157158 "required": ["message"]
158159 }`),
159160 },
160- func(ctx context.Context, args json.RawMessage) (*ToolResult, error) {
161+ func(ctx context.Context, caller CallerContext, args json.RawMessage) (*ToolResult, error) {
161162 var params struct {
162163 Message string `json:"message"`
163164 }
@@ -168,7 +169,7 @@ func RegisterOrchestratorTools(proxy *Proxy, orchestrator Orchestrator) {
168169 }, nil
169170 }
170171
171- if err := orchestrator.BroadcastAsync(params.Message); err != nil {
172+ if err := orchestrator.BroadcastFrom(caller.MysisID, params.Message); err != nil {
172173 return &ToolResult{
173174 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("broadcast failed: %v", err)}},
174175 IsError: true,
@@ -176,7 +177,7 @@ func RegisterOrchestratorTools(proxy *Proxy, orchestrator Orchestrator) {
176177 }
177178
178179 return &ToolResult{
179- Content: []ContentBlock{{Type: "text", Text: "broadcast queued for delivery to all running myses"}},
180+ Content: []ContentBlock{{Type: "text", Text: "broadcast sent to all running myses (excluding you)"}},
180181 }, nil
181182 },
182183 )