Implements the foundational layer that all SynapBus features depend on: - internal/storage: SQLite connection manager (WAL mode, busy_timeout, foreign_keys) and embedded migration runner using modernc.org/sqlite - internal/messaging: MessagingService with send, read inbox, claim, mark done/failed, and FTS5 search. SQLite-backed MessageStore with conversation auto-creation and read/unread tracking via inbox_state. - internal/agents: AgentService with register, authenticate (bcrypt), update, deregister, discover by capability. HTTP auth middleware. - internal/mcp: MCP server using mark3labs/mcp-go with 9 registered tools (send_message, read_inbox, claim_messages, mark_done, search_messages, register_agent, discover_agents, update_agent, deregister_agent). SSE transport, health endpoint, connection manager. - internal/trace: Async trace recorder with buffered channel for recording agent actions to SQLite traces table. - cmd/synapbus: Updated main.go wiring storage, migrations, services, MCP server, chi router, and graceful shutdown. All code compiles with CGO_ENABLED=0. Full test suite passes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36 lines
1002 B
Go
36 lines
1002 B
Go
package mcp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/smart-mcp-proxy/synapbus/internal/agents"
|
|
)
|
|
|
|
type mcpContextKey string
|
|
|
|
const agentNameKey mcpContextKey = "mcp_agent_name"
|
|
|
|
// AgentNameFromContext extracts the agent name from the MCP context.
|
|
func AgentNameFromContext(ctx context.Context) (string, bool) {
|
|
name, ok := ctx.Value(agentNameKey).(string)
|
|
return name, ok
|
|
}
|
|
|
|
// ContextWithAgentName returns a new context with the agent name set.
|
|
func ContextWithAgentName(ctx context.Context, name string) context.Context {
|
|
return context.WithValue(ctx, agentNameKey, name)
|
|
}
|
|
|
|
// extractAgentName gets the agent name from the request context.
|
|
// It first checks for the MCP-level agent name, then falls back to the
|
|
// agents package context (set by HTTP auth middleware).
|
|
func extractAgentName(ctx context.Context) (string, bool) {
|
|
if name, ok := AgentNameFromContext(ctx); ok {
|
|
return name, true
|
|
}
|
|
if agent, ok := agents.AgentFromContext(ctx); ok {
|
|
return agent.Name, true
|
|
}
|
|
return "", false
|
|
}
|