111 lines
3.5 KiB
Go
111 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/chattools"
|
|
)
|
|
|
|
// chatToolTokenStore persists the MCP endpoint tokens (pipeline.Store).
|
|
type chatToolTokenStore interface {
|
|
ChatToolToken(ctx context.Context, scope string) (string, error)
|
|
IssueChatToolToken(ctx context.Context, scope string) (string, error)
|
|
ChatToolScope(ctx context.Context, token string) (string, bool, error)
|
|
BindChatToolToken(ctx context.Context, token, scope string) error
|
|
}
|
|
|
|
// The token scopes.
|
|
const (
|
|
chatToolSessionScope = "session:"
|
|
chatToolPendingScope = "pending:"
|
|
chatToolInstanceScope = "instance:"
|
|
)
|
|
|
|
// chatToolPath is the loopback route prefix of the MCP endpoint. The web proxy
|
|
// and the peer relay forward an allowlist that does not include it.
|
|
const chatToolPath = "/mcp/chat/"
|
|
|
|
// chatToolRegistry issues the endpoint URLs harnesses register
|
|
// (chattools.Registry) on the server's own loopback listener.
|
|
type chatToolRegistry struct {
|
|
store chatToolTokenStore
|
|
base string
|
|
}
|
|
|
|
func (r chatToolRegistry) url(token string) string { return r.base + chatToolPath + token }
|
|
|
|
func (r chatToolRegistry) SessionURL(sessionID string) (string, error) {
|
|
if !validChatSessionID(sessionID) {
|
|
return "", errors.New("invalid chat session id")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
token, err := r.store.ChatToolToken(ctx, chatToolSessionScope+sessionID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return r.url(token), nil
|
|
}
|
|
|
|
func (r chatToolRegistry) PendingURL() (string, func(string) error, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
token, err := r.store.IssueChatToolToken(ctx, chatToolPendingScope)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
bind := func(sessionID string) error {
|
|
if !validChatSessionID(sessionID) {
|
|
return errors.New("invalid chat session id")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
return r.store.BindChatToolToken(ctx, token, chatToolSessionScope+sessionID)
|
|
}
|
|
return r.url(token), bind, nil
|
|
}
|
|
|
|
func (r chatToolRegistry) InstanceURL(harness string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
token, err := r.store.ChatToolToken(ctx, chatToolInstanceScope+harness)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return r.url(token), nil
|
|
}
|
|
|
|
// loopbackBase is the http origin harnesses on this machine use to reach a
|
|
// server listening on addr: a wildcard or blank host becomes 127.0.0.1. A
|
|
// server bound only to a non-loopback address cannot serve the tools: the
|
|
// endpoint answers loopback peers alone.
|
|
func loopbackBase(addr string) (string, error) {
|
|
host, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
ip := net.ParseIP(host)
|
|
switch {
|
|
case host == "localhost" || (ip != nil && ip.IsLoopback()):
|
|
return "http://" + net.JoinHostPort(host, port), nil
|
|
case host == "" || (ip != nil && ip.IsUnspecified()):
|
|
return "http://" + net.JoinHostPort("127.0.0.1", port), nil
|
|
}
|
|
return "", errors.New("the chat tools need a loopback or wildcard listen address, not " + addr)
|
|
}
|
|
|
|
// ConfigureChatTools makes every chat harness Pipelines launches register the
|
|
// Pipelines MCP server on the listener at addr. It is called once at startup,
|
|
// before any harness starts.
|
|
func ConfigureChatTools(store chatToolTokenStore, addr string) error {
|
|
base, err := loopbackBase(addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
chattools.Configure(chatToolRegistry{store: store, base: base})
|
|
return nil
|
|
}
|