feat(kit): central tools for harnesses through the egress proxy and gateway task-tool RPCs

This commit is contained in:
sam committed 2026-10-17 23:42:37 +11:00
1 parent f975dccec0
commit 3f553bb6f2
41 files changed
+2187 -189

No files matched your search

@@ -0,0 +1,61 @@
// Package tasktools adapts task-scoped central tools to the Gateway contract.
package tasktools
import (
"context"
"encoding/json"
"github.com/modelcontextprotocol/go-sdk/mcp"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
)
// Client never accepts an identity, org or permission list from the harness.
type Client struct{ rpc pb.TaskToolsServiceClient }
func NewClient(conn grpc.ClientConnInterface) *Client {
return &Client{rpc: pb.NewTaskToolsServiceClient(conn)}
}
func (c *Client) List(ctx context.Context, credential string) ([]*mcp.Tool, error) {
r, err := c.rpc.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: credential})
if err != nil {
return nil, err
}
var tools []*mcp.Tool
err = json.Unmarshal(r.ToolsJson, &tools)
return tools, err
}
func (c *Client) Call(ctx context.Context, credential, name string, args json.RawMessage) (*mcp.CallToolResult, error) {
r, err := c.rpc.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: credential, Name: name, ArgumentsJson: args})
if err != nil {
return nil, err
}
var result mcp.CallToolResult
err = json.Unmarshal(r.ResultJson, &result)
return &result, err
}
func (c *Client) Close(ctx context.Context, credential string) error {
_, err := c.rpc.CloseTaskTools(ctx, &pb.CloseTaskToolsRequest{RunCredential: credential})
return err
}
// Dial opens the Gateway's task-tools contract as the authenticated daemon.
func Dial(target string, id *rpctls.Identity) (*Client, func(), error) {
creds, err := id.DialOption(target, presence.Gateway)
if err != nil {
return nil, nil, err
}
conn, err := grpc.NewClient(target, creds, grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor(presence.Daemon, "")), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(messaging.MaxInboundAttachmentBytes+(1<<20))))
if err != nil {
return nil, nil, err
}
return NewClient(conn), func() { _ = conn.Close() }, nil
}
+47
View File
@@ -0,0 +1,47 @@
package tasktools
import (
"context"
"crypto/subtle"
"encoding/json"
"net/http"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
const (
Endpoint = "http://archie.tools/mcp"
Sentinel = "archie-task-tools-sentinel"
)
type Backend interface {
List(context.Context, string) ([]*mcp.Tool, error)
Call(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error)
Close(context.Context, string) error
}
// ServeHTTP creates a fresh view so tool discovery never outlives permissions.
func ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, backend Backend, credential, token string) {
if token == "" || subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte("Bearer "+token)) != 1 {
http.Error(w, "invalid gateway credential", http.StatusUnauthorized)
return
}
tools, err := backend.List(ctx, credential)
if err != nil {
http.Error(w, "task tools unavailable", http.StatusBadGateway)
return
}
server := mcp.NewServer(&mcp.Implementation{Name: "archie-central", Version: "1"}, nil)
for _, tool := range tools {
server.AddTool(tool, func(methodCtx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// MCP handler contexts may outlive HTTP; proxy revocation must not.
callCtx, cancel := context.WithCancel(methodCtx)
defer cancel()
stop := context.AfterFunc(ctx, cancel)
defer stop()
return backend.Call(callCtx, credential, req.Params.Name, req.Params.Arguments)
})
}
// The proxy has already authenticated the session and confined Host and path.
mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server }, &mcp.StreamableHTTPOptions{Stateless: true, DisableLocalhostProtection: true}).ServeHTTP(w, r.WithContext(ctx))
}