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>
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
// Package mcp provides the MCP server implementation for SynapBus.
|
|
package mcp
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Connection represents an active agent connection.
|
|
type Connection struct {
|
|
ID string `json:"id"`
|
|
AgentName string `json:"agent_name"`
|
|
Transport string `json:"transport"`
|
|
ConnectedAt time.Time `json:"connected_at"`
|
|
LastActivity time.Time `json:"last_activity"`
|
|
}
|
|
|
|
// ConnectionManager tracks active MCP connections (thread-safe).
|
|
type ConnectionManager struct {
|
|
mu sync.RWMutex
|
|
conns map[string]*Connection
|
|
}
|
|
|
|
// NewConnectionManager creates a new ConnectionManager.
|
|
func NewConnectionManager() *ConnectionManager {
|
|
return &ConnectionManager{
|
|
conns: make(map[string]*Connection),
|
|
}
|
|
}
|
|
|
|
// Add registers a new connection.
|
|
func (cm *ConnectionManager) Add(conn *Connection) {
|
|
cm.mu.Lock()
|
|
defer cm.mu.Unlock()
|
|
cm.conns[conn.ID] = conn
|
|
}
|
|
|
|
// Remove unregisters a connection.
|
|
func (cm *ConnectionManager) Remove(id string) {
|
|
cm.mu.Lock()
|
|
defer cm.mu.Unlock()
|
|
delete(cm.conns, id)
|
|
}
|
|
|
|
// Get returns a connection by ID.
|
|
func (cm *ConnectionManager) Get(id string) (*Connection, bool) {
|
|
cm.mu.RLock()
|
|
defer cm.mu.RUnlock()
|
|
conn, ok := cm.conns[id]
|
|
return conn, ok
|
|
}
|
|
|
|
// Count returns the number of active connections.
|
|
func (cm *ConnectionManager) Count() int {
|
|
cm.mu.RLock()
|
|
defer cm.mu.RUnlock()
|
|
return len(cm.conns)
|
|
}
|
|
|
|
// List returns all active connections.
|
|
func (cm *ConnectionManager) List() []*Connection {
|
|
cm.mu.RLock()
|
|
defer cm.mu.RUnlock()
|
|
result := make([]*Connection, 0, len(cm.conns))
|
|
for _, conn := range cm.conns {
|
|
result = append(result, conn)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// UpdateActivity updates the last activity timestamp for a connection.
|
|
func (cm *ConnectionManager) UpdateActivity(id string) {
|
|
cm.mu.Lock()
|
|
defer cm.mu.Unlock()
|
|
if conn, ok := cm.conns[id]; ok {
|
|
conn.LastActivity = time.Now()
|
|
}
|
|
}
|