feat: 打通 Dispatcher→MCP 工具调用链路 (core NATS request-reply)
第 4 层 Dispatcher 经 NATS request-reply + 队列组同步调用第 5 层 MCP 工具, 工具不可用/超时即降级,不阻断主流程。 - shared/contract: ToolCall/ToolResult + sundynix.tools.go.* subject 约定 + ToolSubjectGo/Py - shared/bus: CallTool(发起) / ServeTool(队列组订阅+应答) - mcp-go: 接共享 bus,gateway 通配订阅按工具名分发(wiki_search/echo),main 优雅退出 - dispatcher: ToolCaller 接口 + Orchestrator.retrieveContext(调 wiki_search,超时3s降级) - e2e: TestToolCallRoundTrip(PASS);demo.sh 加 mcp-go(就绪门避免启动竞态),live 跑通 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ package bus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -133,6 +134,55 @@ func (b *Bus) SubscribeTokens(taskID string, onToken func([]byte), onDone func()
|
||||
return sub.Unsubscribe, nil
|
||||
}
|
||||
|
||||
// ---- MCP 工具调用(core NATS request-reply)----
|
||||
|
||||
// CallTool 同步调用一个 MCP 工具:发到 subject,阻塞等待应答。
|
||||
// ctx 超时即视为工具不可用,由调用方决定降级。
|
||||
func (b *Bus) CallTool(ctx context.Context, subject string, call *contract.ToolCall) (*contract.ToolResult, error) {
|
||||
data, err := json.Marshal(call)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal tool call: %w", err)
|
||||
}
|
||||
msg, err := b.nc.RequestWithContext(ctx, subject, data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("call tool %s: %w", subject, err)
|
||||
}
|
||||
var res contract.ToolResult
|
||||
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal tool result: %w", err)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// ToolHandler 处理一次工具调用并返回结果。
|
||||
type ToolHandler func(ctx context.Context, call *contract.ToolCall) *contract.ToolResult
|
||||
|
||||
// ServeTool 以队列组订阅工具主题(可用通配 sundynix.tools.go.>),
|
||||
// 对每个请求调用 h 并 Respond,队列组内多副本自动负载均衡。
|
||||
// 返回的 unsub 用于退订。
|
||||
func (b *Bus) ServeTool(subject, queue string, h ToolHandler) (unsub func() error, err error) {
|
||||
sub, err := b.nc.QueueSubscribe(subject, queue, func(m *nats.Msg) {
|
||||
var call contract.ToolCall
|
||||
if err := json.Unmarshal(m.Data, &call); err != nil {
|
||||
respond(m, &contract.ToolResult{OK: false, Error: "bad tool call: " + err.Error()})
|
||||
return
|
||||
}
|
||||
respond(m, h(context.Background(), &call))
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serve tool %s: %w", subject, err)
|
||||
}
|
||||
return sub.Unsubscribe, nil
|
||||
}
|
||||
|
||||
func respond(m *nats.Msg, res *contract.ToolResult) {
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
data, _ = json.Marshal(&contract.ToolResult{OK: false, Error: "marshal result: " + err.Error()})
|
||||
}
|
||||
_ = m.Respond(data)
|
||||
}
|
||||
|
||||
// TaskHandler 处理一个消费到的任务。
|
||||
type TaskHandler func(ctx context.Context, t *contract.Task) error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user