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
+21 -3
View File
@@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@@ -307,13 +308,30 @@ func OpenChat(ctx context.Context, executable, directory string) (*Connection, e
if err != nil {
return nil, err
}
if overrides != nil && backend == "v2" {
if c.configContent, err = configContent(os.Getenv("OPENCODE_CONFIG_CONTENT"), overrides); err != nil {
if backend != "v2" {
overrides = nil
}
// The Pipelines chat tools are registered on every spawned chat service.
withTools := withPipelinesTools(overrides)
if withTools != nil {
if c.configContent, err = configContent(os.Getenv("OPENCODE_CONFIG_CONTENT"), withTools); err != nil {
return nil, err
}
}
if err := c.startServe(ctx); err != nil {
return nil, err
if withTools == nil || ctx.Err() != nil {
return nil, err
}
log.Printf("OpenCode chat service did not start with the Pipelines tools (%v); starting without them", err)
c.process, c.configContent = nil, ""
if overrides != nil {
if c.configContent, err = configContent(os.Getenv("OPENCODE_CONFIG_CONTENT"), overrides); err != nil {
return nil, err
}
}
if err := c.startServe(ctx); err != nil {
return nil, err
}
}
return c, nil
}
+30
View File
@@ -0,0 +1,30 @@
package opencode
import (
"log"
"git.a-wan.net/alan/pipelines/internal/chattools"
)
// withPipelinesTools adds the Pipelines MCP server to a chat service's config
// overrides. One OpenCode service serves every chat and its MCP requests name
// no session, so it registers the service's instance endpoint; the server
// attributes each call to the chat whose stream shows it running. The
// operator's own overrides win, including a server they named "pipelines".
// It returns overrides unchanged when the tools are not configured.
func withPipelinesTools(overrides map[string]any) map[string]any {
registry := chattools.Current()
if registry == nil {
return overrides
}
url, err := registry.InstanceURL("opencode")
if err != nil {
log.Printf("OpenCode chat: Pipelines tools not registered: %v", err)
return overrides
}
merged := chattools.OpenCodeConfig(url)
if overrides != nil {
mergeConfig(merged, overrides)
}
return merged
}
+52
View File
@@ -0,0 +1,52 @@
package opencode
import (
"encoding/json"
"errors"
"testing"
"git.a-wan.net/alan/pipelines/internal/chattools"
)
type toolRegistry struct{}
func (toolRegistry) SessionURL(string) (string, error) { return "", errors.New("unused") }
func (toolRegistry) PendingURL() (string, func(string) error, error) {
return "", nil, errors.New("unused")
}
func (toolRegistry) InstanceURL(harness string) (string, error) {
return "http://127.0.0.1:9/mcp/chat/instance-" + harness, nil
}
func TestChatServiceConfigRegistersThePipelinesTools(t *testing.T) {
if got := withPipelinesTools(nil); got != nil {
t.Fatalf("no registry must add nothing: %v", got)
}
chattools.Configure(toolRegistry{})
t.Cleanup(func() { chattools.Configure(nil) })
content, err := configContent(`{"model":"x/y","mcp":{"mine":{"type":"local","command":["m"]}}}`, withPipelinesTools(nil))
if err != nil {
t.Fatal(err)
}
var config struct {
Model string `json:"model"`
MCP map[string]map[string]any `json:"mcp"`
}
if err := json.Unmarshal([]byte(content), &config); err != nil {
t.Fatal(err)
}
if config.Model != "x/y" || config.MCP["mine"] == nil {
t.Fatalf("the operator's config must be kept: %s", content)
}
if got := config.MCP["pipelines"]; got["type"] != "remote" || got["url"] != "http://127.0.0.1:9/mcp/chat/instance-opencode" || got["enabled"] != true {
t.Fatalf("pipelines mcp = %v", got)
}
// Chat overrides the operator set win, including their own "pipelines".
merged := withPipelinesTools(map[string]any{"mcp": map[string]any{"pipelines": map[string]any{"enabled": false}}, "limit": 1.0})
server := merged["mcp"].(map[string]any)["pipelines"].(map[string]any)
if server["enabled"] != false || server["url"] == nil || merged["limit"] != 1.0 {
t.Fatalf("merged = %v", merged)
}
}
+13
View File
@@ -179,6 +179,19 @@ func EnsureOwnedChatService(ctx context.Context, executable, directory string) (
return nil, err
}
}
// The Pipelines chat tools are registered on the chat service; a service
// that does not start with them is started without (logged).
if withTools := withPipelinesTools(overrides); withTools != nil {
toolsContent, err := configContent(os.Getenv("OPENCODE_CONFIG_CONTENT"), withTools)
if err != nil {
return nil, err
}
c, err := ensureOwnedService(ctx, chatOwnedService, toolsContent, executable, directory, nil, "")
if err == nil || ctx.Err() != nil {
return c, err
}
log.Printf("OpenCode chat service did not start with the Pipelines tools (%v); starting without them", err)
}
return ensureOwnedService(ctx, chatOwnedService, content, executable, directory, nil, "")
}