Serve the Pipelines chat tools over a loopback MCP endpoint and register it with Claude Code, Codex and OpenCode chats

This commit is contained in:
2026-11-09 16:34:21 +00:00
parent 3ab029cd7b
commit 9791f09230
33 changed files with 2308 additions and 7 deletions
+2 -2
View File
@@ -461,7 +461,7 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess
var refused *rpcCallError
if errors.As(err, &refused) && refused.Message == "thread not found: "+id {
var resumed json.RawMessage
resumed, err = c.call(ctx, "thread/resume", map[string]any{"threadId": id, "excludeTurns": true})
resumed, err = c.resumeThread(ctx, map[string]any{"threadId": id, "excludeTurns": true}, id)
if err == nil {
var response struct {
Thread struct {
@@ -549,7 +549,7 @@ func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.Ch
if strings.TrimSpace(directory) == "" {
return protocol.ChatSession{}, errors.New("a directory is required")
}
raw, err := c.call(ctx, "thread/start", map[string]any{
raw, err := c.startThread(ctx, map[string]any{
"cwd": directory,
"sandbox": "workspace-write",
})
+76
View File
@@ -0,0 +1,76 @@
package codex
import (
"context"
"encoding/json"
"errors"
"log"
"maps"
"git.a-wan.net/alan/pipelines/internal/chattools"
)
// withPipelinesTools returns params with the thread config override that
// registers the Pipelines MCP server at url, leaving params unchanged.
func withPipelinesTools(params map[string]any, url string) map[string]any {
next := maps.Clone(params)
next["config"] = chattools.CodexConfig(url)
return next
}
// startThread calls thread/start registering the Pipelines tools for the new
// thread. Its id is assigned by the App Server, so the endpoint is issued
// pending and bound to the id the reply names. An App Server that refuses the
// override still gets the thread, started without the tools (logged).
func (c *Chat) startThread(ctx context.Context, params map[string]any) (json.RawMessage, error) {
registry := chattools.Current()
if registry == nil {
return c.call(ctx, "thread/start", params)
}
url, bind, err := registry.PendingURL()
if err != nil {
log.Printf("codex chat: Pipelines tools not registered: %v", err)
return c.call(ctx, "thread/start", params)
}
raw, err := c.call(ctx, "thread/start", withPipelinesTools(params, url))
var refused *rpcCallError
if errors.As(err, &refused) {
log.Printf("codex chat: thread/start refused the Pipelines tools (%s); starting without them", refused.Message)
return c.call(ctx, "thread/start", params)
}
if err != nil {
return raw, err
}
var reply struct {
Thread struct {
ID string `json:"id"`
} `json:"thread"`
}
if json.Unmarshal(raw, &reply) == nil && reply.Thread.ID != "" {
if err := bind(reply.Thread.ID); err != nil {
log.Printf("codex chat %s: Pipelines tools endpoint not bound: %v", reply.Thread.ID, err)
}
}
return raw, nil
}
// resumeThread calls thread/resume for id, registering the thread's own
// Pipelines tools endpoint; a refusal of the override resumes without it.
func (c *Chat) resumeThread(ctx context.Context, params map[string]any, id string) (json.RawMessage, error) {
registry := chattools.Current()
if registry == nil {
return c.call(ctx, "thread/resume", params)
}
url, err := registry.SessionURL(id)
if err != nil {
log.Printf("codex chat %s: Pipelines tools not registered: %v", id, err)
return c.call(ctx, "thread/resume", params)
}
raw, err := c.call(ctx, "thread/resume", withPipelinesTools(params, url))
var refused *rpcCallError
if errors.As(err, &refused) && refused.Message != "thread not found: "+id {
log.Printf("codex chat %s: thread/resume refused the Pipelines tools (%s); resuming without them", id, refused.Message)
return c.call(ctx, "thread/resume", params)
}
return raw, err
}
+100
View File
@@ -0,0 +1,100 @@
package codex
import (
"context"
"encoding/json"
"errors"
"sync"
"testing"
"git.a-wan.net/alan/pipelines/internal/chattools"
)
type toolRegistry struct {
mu sync.Mutex
bound []string
}
func (r *toolRegistry) SessionURL(id string) (string, error) {
return "http://127.0.0.1:9/mcp/chat/session-" + id, nil
}
func (r *toolRegistry) PendingURL() (string, func(string) error, error) {
return "http://127.0.0.1:9/mcp/chat/pending", func(id string) error {
r.mu.Lock()
defer r.mu.Unlock()
r.bound = append(r.bound, id)
return nil
}, nil
}
func (r *toolRegistry) InstanceURL(string) (string, error) { return "", errors.New("unused") }
func mcpURL(t *testing.T, raw json.RawMessage) string {
t.Helper()
var params struct {
Config struct {
MCPServers map[string]struct {
URL string `json:"url"`
} `json:"mcp_servers"`
} `json:"config"`
}
if err := json.Unmarshal(raw, &params); err != nil {
t.Fatal(err)
}
return params.Config.MCPServers["pipelines"].URL
}
func TestThreadStartRegistersAndBindsThePipelinesTools(t *testing.T) {
registry := &toolRegistry{}
chattools.Configure(registry)
t.Cleanup(func() { chattools.Configure(nil) })
var urls []string
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
switch method {
case "thread/start":
urls = append(urls, mcpURL(t, raw))
return json.RawMessage(`{"thread":{"id":"thr-1","cwd":"/w"},"model":"gpt","modelProvider":"openai"}`)
case "thread/resume":
urls = append(urls, mcpURL(t, raw))
return json.RawMessage(`{"thread":{"id":"thr-1"}}`)
}
return json.RawMessage(`{}`)
})
if _, err := chat.Create(context.Background(), "", "/w"); err != nil {
t.Fatal(err)
}
if _, err := chat.resumeThread(context.Background(), map[string]any{"threadId": "thr-1"}, "thr-1"); err != nil {
t.Fatal(err)
}
if len(urls) != 2 || urls[0] != "http://127.0.0.1:9/mcp/chat/pending" || urls[1] != "http://127.0.0.1:9/mcp/chat/session-thr-1" {
t.Fatalf("registered urls = %v", urls)
}
if len(registry.bound) != 1 || registry.bound[0] != "thr-1" {
t.Fatalf("the pending endpoint must be bound to the new thread: %v", registry.bound)
}
}
func TestThreadStartWithoutToolsWhenTheAppServerRefusesThem(t *testing.T) {
chattools.Configure(&toolRegistry{})
t.Cleanup(func() { chattools.Configure(nil) })
var attempts []string
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
if method != "thread/start" {
return json.RawMessage(`{}`)
}
url := mcpURL(t, raw)
attempts = append(attempts, url)
if url != "" {
return &rpcError{Code: -32602, Message: "invalid config: mcp_servers"}
}
return json.RawMessage(`{"thread":{"id":"thr-2","cwd":"/w"}}`)
})
session, err := chat.Create(context.Background(), "", "/w")
if err != nil || session.ID != "thr-2" {
t.Fatalf("the chat must still start: %v %v", session, err)
}
if len(attempts) != 2 || attempts[0] == "" || attempts[1] != "" {
t.Fatalf("attempts = %q", attempts)
}
}