mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
8caf9aeb2b
Migrate hook observation to runtime events and update the process hook notification protocol. Add runtime event publication for message bus failures, channel lifecycle/outbound flow, gateway reloads, MCP server state, and MCP tool calls. Validation: go test ./pkg/events/... ./pkg/bus ./pkg/agent ./pkg/channels ./pkg/mcp ./pkg/tools/integration ./pkg/gateway; make lint
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
runtimeevents "github.com/sipeed/picoclaw/pkg/events"
|
|
)
|
|
|
|
const mcpEventPublishTimeout = 100 * time.Millisecond
|
|
|
|
func (m *Manager) publishServerEvent(
|
|
kind runtimeevents.Kind,
|
|
serverName string,
|
|
cfg config.MCPServerConfig,
|
|
toolCount int,
|
|
err error,
|
|
) {
|
|
if m == nil || m.runtimeEvents == nil {
|
|
return
|
|
}
|
|
|
|
severity := runtimeevents.SeverityInfo
|
|
if err != nil {
|
|
severity = runtimeevents.SeverityError
|
|
}
|
|
payload := ServerEventPayload{
|
|
Server: serverName,
|
|
Type: mcpTransportType(cfg),
|
|
URL: cfg.URL,
|
|
Command: cfg.Command,
|
|
ToolCount: toolCount,
|
|
}
|
|
if err != nil {
|
|
payload.Error = err.Error()
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), mcpEventPublishTimeout)
|
|
defer cancel()
|
|
m.runtimeEvents.Publish(ctx, runtimeevents.Event{
|
|
Kind: kind,
|
|
Source: runtimeevents.Source{Component: "mcp", Name: serverName},
|
|
Severity: severity,
|
|
Payload: payload,
|
|
})
|
|
}
|
|
|
|
func (m *Manager) publishToolDiscovered(serverName string, cfg config.MCPServerConfig, toolName string) {
|
|
if m == nil || m.runtimeEvents == nil {
|
|
return
|
|
}
|
|
payload := ServerEventPayload{
|
|
Server: serverName,
|
|
Type: mcpTransportType(cfg),
|
|
URL: cfg.URL,
|
|
Command: cfg.Command,
|
|
Tool: toolName,
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), mcpEventPublishTimeout)
|
|
defer cancel()
|
|
m.runtimeEvents.Publish(ctx, runtimeevents.Event{
|
|
Kind: runtimeevents.KindMCPToolDiscovered,
|
|
Source: runtimeevents.Source{Component: "mcp", Name: serverName},
|
|
Severity: runtimeevents.SeverityInfo,
|
|
Payload: payload,
|
|
})
|
|
}
|
|
|
|
func mcpTransportType(cfg config.MCPServerConfig) string {
|
|
if cfg.Type != "" {
|
|
return cfg.Type
|
|
}
|
|
if cfg.URL != "" {
|
|
return "sse"
|
|
}
|
|
if cfg.Command != "" {
|
|
return "stdio"
|
|
}
|
|
return ""
|
|
}
|