feat(kit): central tools for harnesses through the egress proxy and gateway task-tool RPCs
This commit is contained in:
41 files changed
+2187
-189
No files matched your search
@@ -40,8 +40,9 @@ Archie is a partial Kit runtime. It implements these capability types:
|
||||
| `volume@1` | A Docker volume keyed by WorkflowExecution, deleted with it. |
|
||||
| `lifecycle@1` | Install and startup hooks run on container create. |
|
||||
| `agent-sessions@1` | The headless `prompt` and `resume` invocations for the stage. |
|
||||
| `agent-skills@1` | The operator's skills store (`skills_dir`), mounted read-only whatever the Kit asks. |
|
||||
| `agent-context@1` | The workload's profile file beside the workspace, indexing each Kit's guidance. |
|
||||
| `agent-skills@1` | Discovery paths for bundled skills and optional shared skills; host access is always read-only. |
|
||||
| `agent-skill@1` | Selected image-bundled skills copied into the declared discovery paths. |
|
||||
| `agent-context@1` | The resolved discovery directory, defaulting beside the workspace; preserve instructions outside Archie-managed sections. |
|
||||
| `resources@1` | Container CPU and memory limits. |
|
||||
|
||||
Every other type fails closed: a required entry refuses the launch, an
|
||||
@@ -258,8 +259,8 @@ carries.
|
||||
- **Proxy.** The proxy runs inside `archied`, listening on the host gateway
|
||||
of the host-reachable network. It terminates TLS with a per-daemon CA
|
||||
installed into the container at create, identifies the container by a
|
||||
per-run proxy token, and applies that run's network policy and credential
|
||||
inject rules. Upstream dials refuse loopback, and refuse internal addresses
|
||||
per-run proxy password distinct from the worker's run grant, and applies
|
||||
that run's network policy and credential inject rules. Upstream dials refuse loopback, and refuse internal addresses
|
||||
unless the policy names the exact IP, so an allowed name cannot resolve
|
||||
back into the host.
|
||||
|
||||
@@ -294,12 +295,34 @@ the run and not with how many agents it fans out to.
|
||||
|
||||
## Archie tools inside the harness
|
||||
|
||||
Capture tools, which are how a stage returns structured results, are served
|
||||
by `archie-agent mcp`, a stdio MCP server registered through the Kit's MCP
|
||||
configuration or the adapter's flags. It serves the stage's `CaptureTools`
|
||||
and the central tools the agent profile's `AllowTools` permits, never the
|
||||
`workspace` toolset. The runner's `ToolLimits` bound its results; the
|
||||
harness's own built-in tools are outside archie's limits.
|
||||
Capture tools return a stage's structured results through `archie-agent mcp`,
|
||||
a stdio MCP server. The same command forwards central-tool discovery and
|
||||
calls over Streamable HTTP to `http://archie.tools/mcp` through the egress
|
||||
proxy. Claude Code, Codex, Pi, OMP and GitHub Copilot CLI register it using
|
||||
their native MCP configuration. Registration files are per-stage and outside
|
||||
the worktree; the Kit's bundled settings remain intact. A CLI that exits
|
||||
without initializing the required MCP server fails the stage.
|
||||
|
||||
`archied` handles that exact endpoint inside the authenticated task proxy;
|
||||
it is not a listener or an upstream network destination. It is available
|
||||
only during task runtime, never during install or a setup session. The
|
||||
harness supplies a sentinel, and the proxy injects a separate random gateway
|
||||
token held only on the host. Ending the proxy session revokes that token and
|
||||
cancels active calls.
|
||||
|
||||
The proxy reaches `archie-gateway` through its authenticated gRPC contract.
|
||||
The Gateway derives the task, execution principal and org from a live run
|
||||
grant on every request. Its tool list is bounded by the profile in that org
|
||||
and the package's accepted tool authority, checked again at invocation.
|
||||
Workspace tools, chat file delivery, conversation-only tools and tools that
|
||||
require an unavailable approver are not served. Tool result limits apply
|
||||
without exposing host spill paths; media is returned as MCP content.
|
||||
|
||||
Task MCP providers run in the Gateway with task-bound sessions and sampling.
|
||||
Sampling uses the task's model and credentials, records task usage and refuses
|
||||
revoked grants. It does not use the chat identity or chat model. The worker
|
||||
does not start another copy of those providers for Kit tasks. The harness's
|
||||
own built-in tools remain outside Archie's tool-result limits.
|
||||
|
||||
## Enforcement around the harness
|
||||
|
||||
@@ -360,6 +383,17 @@ of them.
|
||||
|
||||
## Verification
|
||||
|
||||
- Every named adapter discovers and calls a permitted central tool through
|
||||
the proxy and records a local capture. A guessed tool outside the profile
|
||||
or accepted package authority is refused.
|
||||
- The gateway token never appears in the container, generated MCP files,
|
||||
worktree, logs or events. It is not the container's proxy password.
|
||||
- A permission change after discovery takes effect at the next call; ending
|
||||
the proxy session cancels active calls, and a revoked grant cannot execute
|
||||
tools or sample a model.
|
||||
- Bundled skills remain visible with shared skills missing or disabled;
|
||||
shared mounts are per-skill and read-only. Explicit context directories
|
||||
preserve non-managed instructions and refuse symlinked staging paths.
|
||||
- The same workflow definition completes on `builtin` and on the Claude Code
|
||||
Kit with the same `Result` shape and captures.
|
||||
- After `/login` in the setup terminal, the container's credential file holds
|
||||
|
||||
@@ -46,6 +46,17 @@ func AcceptCapture(spec agentrun.CaptureTool, accepted int, value json.RawMessag
|
||||
// its rejection, so the agent can correct it, and is not recorded.
|
||||
func ServeCaptureMCP(ctx context.Context, specs []agentrun.CaptureTool, sink io.Writer, t mcp.Transport) error {
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "archie", Version: "1"}, nil)
|
||||
return serveHarnessMCP(ctx, specs, sink, t, server, false)
|
||||
}
|
||||
|
||||
func serveHarnessMCP(ctx context.Context, specs []agentrun.CaptureTool, sink io.Writer, t mcp.Transport, server *mcp.Server, central bool) error {
|
||||
if central {
|
||||
cleanup, err := addCentralTools(ctx, server, specs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
}
|
||||
var mu sync.Mutex
|
||||
accepted := map[string]int{}
|
||||
for _, spec := range specs {
|
||||
@@ -121,8 +132,8 @@ func ServeCaptureFiles(ctx context.Context, specPath, capturesPath string, t mcp
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var specs []agentrun.CaptureTool
|
||||
if err := json.Unmarshal(raw, &specs); err != nil {
|
||||
var spec harnessMCPSpec
|
||||
if err := json.Unmarshal(raw, &spec); err != nil {
|
||||
return fmt.Errorf("capture tool spec: %w", err)
|
||||
}
|
||||
sink, err := os.OpenFile(capturesPath, os.O_WRONLY|os.O_APPEND, 0)
|
||||
@@ -130,5 +141,17 @@ func ServeCaptureFiles(ctx context.Context, specPath, capturesPath string, t mcp
|
||||
return err
|
||||
}
|
||||
defer sink.Close()
|
||||
return ServeCaptureMCP(ctx, specs, sink, t)
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "archie", Version: "1"}, nil)
|
||||
server.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
|
||||
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
|
||||
result, err := next(ctx, method, req)
|
||||
if err == nil && (method == "tools/list" || method == "tools/call") {
|
||||
if err := os.WriteFile(capturesPath+".ready", nil, 0o600); err != nil {
|
||||
return nil, fmt.Errorf("record MCP initialization: %w", err)
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
})
|
||||
return serveHarnessMCP(ctx, spec.Captures, sink, t, server, spec.CentralTools)
|
||||
}
|
||||
@@ -29,12 +29,9 @@ var harnessAdapters = map[string]HarnessAdapter{
|
||||
NewOutput: func() HarnessOutput { return newClaudeCodeHarnessOutput() },
|
||||
MCPConfig: []string{"--mcp-config", mcpConfigPlaceholder},
|
||||
},
|
||||
// Codex takes MCP servers only as inline -c overrides, and Pi and OMP
|
||||
// read no MCP config file of this shape, so their stages serve no
|
||||
// capture tools.
|
||||
AdapterCodex: {NewOutput: func() HarnessOutput { return &codexHarnessOutput{} }},
|
||||
AdapterPi: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }},
|
||||
AdapterOMP: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }},
|
||||
AdapterCodex: {NewOutput: func() HarnessOutput { return &codexHarnessOutput{} }, MCPConfig: []string{"-c", mcpConfigPlaceholder}},
|
||||
AdapterPi: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }, MCPConfig: []string{"--extension", mcpConfigPlaceholder}},
|
||||
AdapterOMP: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }, MCPConfig: []string{"--extension", mcpConfigPlaceholder}},
|
||||
AdapterCopilot: {
|
||||
NewOutput: func() HarnessOutput { return newCopilotHarnessOutput() },
|
||||
MCPConfig: []string{"--additional-mcp-config", "@" + mcpConfigPlaceholder},
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
||||
)
|
||||
|
||||
type harnessMCPSpec struct {
|
||||
Captures []agentrun.CaptureTool `json:"captures"`
|
||||
CentralTools bool `json:"central_tools"`
|
||||
}
|
||||
|
||||
// Each adapter registers one stdio bridge; it holds only proxy credentials
|
||||
// and sentinels, and never writes invocation configuration into the worktree.
|
||||
func harnessMCPRegistration(adapter, dir, configPath string, server map[string]any) (string, error) {
|
||||
switch adapter {
|
||||
case AdapterCodex:
|
||||
args, err := json.Marshal(server["args"])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
command, ok := server["command"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("MCP command is missing")
|
||||
}
|
||||
return "mcp_servers.archie={command=" + strconv.Quote(command) + ",args=" + string(args) + ",env={HTTP_PROXY=" + strconv.Quote(harnessMCPEnvironmentValue(server)) + "},required=true}", nil
|
||||
case AdapterPi:
|
||||
server["exposure"] = "direct"
|
||||
definition, err := json.Marshal(server)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := filepath.Join(dir, "archie-mcp.ts")
|
||||
script := "export default function(pi) { if (typeof pi.registerMcpServer !== 'function') throw new Error('This Pi version does not support native MCP registration'); pi.registerMcpServer('archie', " + string(definition) + "); }\n"
|
||||
if err := os.WriteFile(path, []byte(script), 0o644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
case AdapterOMP:
|
||||
// OMP discovers .mcp.json in an explicitly loaded extension package.
|
||||
path := filepath.Join(dir, "index.ts")
|
||||
if err := os.WriteFile(path, []byte("export default function() {}\n"), 0o644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, ".mcp.json"), data, 0o644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dir, nil
|
||||
default:
|
||||
return configPath, nil
|
||||
}
|
||||
}
|
||||
|
||||
type centralTransport struct{ base http.RoundTripper }
|
||||
|
||||
func (t centralTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.String() != tasktools.Endpoint {
|
||||
return nil, fmt.Errorf("central tool request names an unexpected endpoint")
|
||||
}
|
||||
r = r.Clone(r.Context())
|
||||
r.Header.Set("Authorization", "Bearer "+tasktools.Sentinel)
|
||||
return t.base.RoundTrip(r)
|
||||
}
|
||||
|
||||
func addCentralTools(ctx context.Context, server *mcp.Server, captures []agentrun.CaptureTool) (func(), error) {
|
||||
proxy, err := url.Parse(os.Getenv("HTTP_PROXY"))
|
||||
if err != nil || proxy.Host == "" {
|
||||
return nil, fmt.Errorf("central tools require the task egress proxy")
|
||||
}
|
||||
transport := &http.Transport{Proxy: http.ProxyURL(proxy)}
|
||||
client := mcp.NewClient(&mcp.Implementation{Name: "archie-harness", Version: "1"}, nil)
|
||||
session, err := client.Connect(ctx, &mcp.StreamableClientTransport{Endpoint: tasktools.Endpoint, HTTPClient: &http.Client{Transport: centralTransport{base: transport}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, DisableStandaloneSSE: true, MaxRetries: -1, MaxEventSize: -1}, nil)
|
||||
if err != nil {
|
||||
transport.CloseIdleConnections()
|
||||
return nil, fmt.Errorf("connect central tools: %w", err)
|
||||
}
|
||||
closeSession := func() { _ = session.Close(); transport.CloseIdleConnections() }
|
||||
names := map[string]bool{}
|
||||
for _, spec := range captures {
|
||||
names[spec.Name] = true
|
||||
}
|
||||
for tool, err := range session.Tools(ctx, nil) {
|
||||
if err != nil {
|
||||
closeSession()
|
||||
return nil, fmt.Errorf("list central tools: %w", err)
|
||||
}
|
||||
if names[tool.Name] {
|
||||
closeSession()
|
||||
return nil, fmt.Errorf("central tool %q collides with a capture tool", tool.Name)
|
||||
}
|
||||
server.AddTool(tool, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
return session.CallTool(ctx, &mcp.CallToolParams{Name: req.Params.Name, Arguments: req.Params.Arguments})
|
||||
})
|
||||
}
|
||||
return closeSession, nil
|
||||
}
|
||||
|
||||
func harnessMCPEnvironment(env []string) map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, entry := range env {
|
||||
if value, ok := strings.CutPrefix(entry, "HTTP_PROXY="); ok {
|
||||
out["HTTP_PROXY"] = value
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func harnessMCPEnvironmentValue(server map[string]any) string {
|
||||
env, _ := server["env"].(map[string]string)
|
||||
return env["HTTP_PROXY"]
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//go:build unix
|
||||
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
)
|
||||
|
||||
// Some CLIs log an unsupported MCP configuration but still exit successfully.
|
||||
func TestHarnessRequiresMCPInitialization(t *testing.T) {
|
||||
for _, central := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "captures", true: "central tools"}[central], func(t *testing.T) {
|
||||
workspace, err := os.MkdirTemp("", "archie-mcp-regression-") //nolint:usetesting // The harness user cannot traverse testing's private parent directory.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(workspace)
|
||||
for _, args := range [][]string{{"init", "-q"}, {"-c", "user.name=fixture", "-c", "user.email=fixture@example.invalid", "commit", "--allow-empty", "-q", "-m", "fixture"}} {
|
||||
cmd := exec.CommandContext(t.Context(), "git", append([]string{"-C", workspace}, args...)...)
|
||||
if raw, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git fixture %v: %s", err, raw)
|
||||
}
|
||||
}
|
||||
h := &agentrun.HarnessSpec{Launch: []string{"sh", "-c", "exit 0", "harness"}, Prompt: []string{"{{.Prompt}}"}, MCPConfig: []string{"{{.MCPConfig}}"}, Env: []string{"PATH=/usr/bin:/bin"}, CentralTools: central}
|
||||
if os.Geteuid() == 0 {
|
||||
h.User = "65534"
|
||||
}
|
||||
req := agentrun.Request{Version: agentrun.ProtocolVersion, TaskID: 7, Attempt: 1, Stage: "test", Mission: "return a result", Model: "fixture/model", Harness: h}
|
||||
if !central {
|
||||
req.CaptureTools = []agentrun.CaptureTool{{Name: "capture_result", Parameters: json.RawMessage(`{"type":"object"}`)}}
|
||||
}
|
||||
result, err := NewHarnessRunner(nil).Run(t.Context(), workspace, req, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Status == agentrun.StatusPassed || result.StopReason != StopExited {
|
||||
t.Fatalf("unsupported MCP silently passed: %+v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -81,6 +81,11 @@ func (r *HarnessRunner) Run(ctx context.Context, workspace string, req agentrun.
|
||||
defer cleanup()
|
||||
res, err := r.run(ctx, workspace, req, mcpConfig, report)
|
||||
if capturesPath != "" {
|
||||
if res.Status == agentrun.StatusPassed {
|
||||
if _, readyErr := os.Stat(capturesPath + ".ready"); readyErr != nil {
|
||||
return park(res, StopExited, "harness did not initialize Archie MCP; check CLI MCP support and startup errors"), err
|
||||
}
|
||||
}
|
||||
captures, readErr := readCaptures(capturesPath, req.CaptureTools)
|
||||
if readErr != nil {
|
||||
return res, errors.Join(err, fmt.Errorf("read captures: %w", readErr))
|
||||
@@ -195,8 +200,8 @@ func validateHarness(req agentrun.Request) error {
|
||||
if len(h.Resume) > 0 && !slices.ContainsFunc(h.Resume, func(s string) bool { return strings.Contains(s, sessionPlaceholder) }) {
|
||||
return fmt.Errorf("harness resume verb must carry %s", sessionPlaceholder)
|
||||
}
|
||||
if len(req.CaptureTools) > 0 && !slices.ContainsFunc(h.MCPConfig, func(s string) bool { return strings.Contains(s, mcpConfigPlaceholder) }) {
|
||||
return errors.New("stage has capture tools but the harness registers no MCP server; its results would be lost")
|
||||
if (len(req.CaptureTools) > 0 || h.CentralTools) && !slices.ContainsFunc(h.MCPConfig, func(s string) bool { return strings.Contains(s, mcpConfigPlaceholder) }) {
|
||||
return errors.New("stage needs Archie tools but the harness registers no MCP server; its results would be lost")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -485,7 +490,7 @@ func (t *tailBuffer) String() string { return t.buf.String() }
|
||||
// writes into it.
|
||||
func (r *HarnessRunner) prepareCaptures(req agentrun.Request) (config, captures string, cleanup func(), err error) {
|
||||
cleanup = func() {}
|
||||
if len(req.CaptureTools) == 0 {
|
||||
if len(req.CaptureTools) == 0 && !req.Harness.CentralTools {
|
||||
return "", "", cleanup, nil
|
||||
}
|
||||
dir, err := os.MkdirTemp("", "archie-captures-")
|
||||
@@ -497,7 +502,7 @@ func (r *HarnessRunner) prepareCaptures(req agentrun.Request) (config, captures
|
||||
cleanup()
|
||||
return "", "", func() {}, err
|
||||
}
|
||||
spec, err := json.Marshal(req.CaptureTools)
|
||||
spec, err := json.Marshal(harnessMCPSpec{Captures: req.CaptureTools, CentralTools: req.Harness.CentralTools})
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
@@ -511,6 +516,7 @@ func (r *HarnessRunner) prepareCaptures(req agentrun.Request) (config, captures
|
||||
command = []string{self}
|
||||
}
|
||||
server := map[string]any{
|
||||
"env": harnessMCPEnvironment(req.Harness.Env),
|
||||
"command": command[0],
|
||||
"args": slices.Concat(command[1:], []string{"mcp", "-spec", specPath, "-captures", capturesPath}),
|
||||
}
|
||||
@@ -526,5 +532,9 @@ func (r *HarnessRunner) prepareCaptures(req agentrun.Request) (config, captures
|
||||
if err := ownForHarness(req.Harness.User, dir, specPath, configPath, capturesPath); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return configPath, capturesPath, cleanup, nil
|
||||
registration, err := harnessMCPRegistration(req.Harness.Adapter, dir, configPath, server)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return registration, capturesPath, cleanup, nil
|
||||
}
|
||||
@@ -14,6 +14,12 @@ import (
|
||||
// "provider/model"; its Reasoning flag tells the runtime which models need
|
||||
// max_completion_tokens instead of max_tokens.
|
||||
func NewRuntime(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits) *runtime.Runtime {
|
||||
return NewRuntimeWithKeys(providers, limits, nil)
|
||||
}
|
||||
|
||||
// NewRuntimeWithKeys binds host-resolved credentials to one runtime without
|
||||
// exporting them into the process environment or the task protocol.
|
||||
func NewRuntimeWithKeys(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits, keys map[string]string) *runtime.Runtime {
|
||||
if len(providers) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -21,7 +27,9 @@ func NewRuntime(providers map[string]agentrun.Provider, limits map[string]config
|
||||
catalog := make(map[string]runtime.ProviderConfig, len(providers))
|
||||
for name, provider := range providers {
|
||||
cfg := runtime.ProviderConfig{ID: name, Class: provider.Class, BaseURL: provider.BaseURL, Models: reasoningModels(name, limits)}
|
||||
if provider.APIKeyEnv == "" {
|
||||
if key, ok := keys[name]; ok {
|
||||
cfg.Auth = runtime.AuthConfig{APIKey: key}
|
||||
} else if provider.APIKeyEnv == "" {
|
||||
cfg.Auth = runtime.AuthConfig{Type: runtime.AuthTypeNone}
|
||||
} else {
|
||||
cfg.Auth = runtime.AuthConfig{APIKeyEnv: provider.APIKeyEnv}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/prsource"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskmcp"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
@@ -177,14 +178,14 @@ func applyToolLimits(agent agentrun.Runner, policy config.ToolPolicy, allow []st
|
||||
// stageRunners builds the runner every agent stage uses. llm is the task's
|
||||
// shared model runtime (the one its MCP providers answer sampling from); the
|
||||
// registry path reuses it rather than building a second one.
|
||||
func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerFactory, llm *runtime.Runtime, log *slog.Logger) (agentrun.Runner, error) {
|
||||
func stageRunners(req taskrun.Request, mcpSet *taskmcp.Set, newRunner runnerFactory, llm *runtime.Runtime, log *slog.Logger) (agentrun.Runner, error) {
|
||||
if req.Harness != nil {
|
||||
// The built-in loop has no route to a model from a Kit container.
|
||||
return agentexec.HarnessStages{Runner: agentexec.NewHarnessRunner(nil), Spec: *req.Harness}, nil
|
||||
}
|
||||
var agent agentrun.Runner
|
||||
if mcpSet != nil && mcpSet.registry != nil {
|
||||
agent = modelloop.NewLoopRunner(llm, log, mcpSet.registry)
|
||||
if mcpSet != nil && mcpSet.Registry != nil {
|
||||
agent = modelloop.NewLoopRunner(llm, log, mcpSet.Registry)
|
||||
} else {
|
||||
agent = newRunner(req.Providers, req.Cfg.ModelLimits, log)
|
||||
}
|
||||
@@ -224,19 +225,9 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
}
|
||||
defer restoreWorktreeOwnership(trees, workDir, log)()
|
||||
|
||||
// The MCP providers a task hosts share the same model runtime its agent
|
||||
// stages run on, so a server-initiated sampling request is answered from
|
||||
// the task's own model rather than a second provider client.
|
||||
var llm *runtime.Runtime
|
||||
if len(req.MCPServers) > 0 {
|
||||
llm = modelloop.NewRuntime(req.Providers, req.Cfg.ModelLimits)
|
||||
}
|
||||
mcpSet, mcpErr := startMCPProviders(ctx, req.MCPServers, taskSamplingHandler(llm, req.Cfg, samplingUsageRecorder(dependencies.store, req.Task, log)), log)
|
||||
llm, mcpSet := taskMCPProviders(ctx, req, dependencies, log)
|
||||
if mcpSet != nil {
|
||||
defer mcpSet.cleanup(ctx, log)
|
||||
}
|
||||
if mcpErr != nil {
|
||||
log.Warn("mcp providers had errors, continuing with available tools", "err", mcpErr)
|
||||
defer mcpSet.Close(ctx, log)
|
||||
}
|
||||
|
||||
agent, err := stageRunners(req, mcpSet, newRunner, llm, log)
|
||||
@@ -342,3 +333,15 @@ func CompilePinnedWorkflow(req *taskrun.Request, steps *workflow.Manager) (workf
|
||||
}
|
||||
return wf, nil
|
||||
}
|
||||
|
||||
func taskMCPProviders(ctx context.Context, req taskrun.Request, deps taskDependencies, log *slog.Logger) (*runtime.Runtime, *taskmcp.Set) {
|
||||
if req.Harness != nil || len(req.MCPServers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
llm := modelloop.NewRuntime(req.Providers, req.Cfg.ModelLimits)
|
||||
set, err := taskmcp.Start(ctx, req.MCPServers, taskmcp.SamplingHandler(llm, req.Cfg, taskmcp.UsageRecorder(deps.store, req.Task, log)), log, storage.MCPNPMCacheMountDir)
|
||||
if err != nil {
|
||||
log.Warn("mcp providers had errors, continuing with available tools", "err", err)
|
||||
}
|
||||
return llm, set
|
||||
}
|
||||
@@ -13,9 +13,12 @@ import (
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/fetch"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/presence"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
||||
)
|
||||
|
||||
const relayName = "archie-egress-relay"
|
||||
@@ -62,7 +65,14 @@ func (b *boot) setupKitLauncher(ctx context.Context) {
|
||||
// Without a harness secret store, Register refuses a Kit with a
|
||||
// required OAuth credential.
|
||||
oauthStore, _ := b.stateStore.(egress.OAuthStore)
|
||||
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: resolver, OAuthStore: oauthStore})
|
||||
gateway, cleanup, err := b.connectTaskTools()
|
||||
if err != nil {
|
||||
_ = ln.Close()
|
||||
log.Warn("kit task tools unavailable", "err", err)
|
||||
return
|
||||
}
|
||||
b.addCleanup(cleanup)
|
||||
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: resolver, OAuthStore: oauthStore, TaskTools: gateway})
|
||||
srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second}
|
||||
go func() {
|
||||
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
@@ -107,3 +117,11 @@ func shutdownEgress(networks *egress.Networks, srv *http.Server, log *slog.Logge
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *boot) connectTaskTools() (*tasktools.Client, func(), error) {
|
||||
id, err := servicekit.Identity(b.cfg.TLS, presence.Daemon)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return tasktools.Dial(b.cfg.Services.Get(config.ServiceNameGateway).Target, id)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
@@ -68,4 +69,4 @@ func (m modelEgress) Open(token, org string, providers map[string]config.Provide
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m modelEgress) Close(token string) { m.proxy.Revoke(token) }
|
||||
func (m modelEgress) Close(ctx context.Context, token string) { m.proxy.Revoke(ctx, token) }
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -30,7 +31,9 @@ func (b *server) serveGatewayListener(ctx context.Context, listener net.Listener
|
||||
// begins accepting the moment it is called, and a unit whose
|
||||
// TimeoutStartSec expires first would restart a healthy process.
|
||||
b.announceReady()
|
||||
return serveGateway(ctx, listener, contract, b.chatSessionStore, b.chatCatalog(), opts)
|
||||
taskTools := &taskToolsServer{gateway: b, authority: b.stateStore, configFor: b.taskToolsConfig, sessions: make(map[[32]byte]*taskToolSession)}
|
||||
defer taskTools.Close(ctx)
|
||||
return serveGateway(ctx, listener, contract, b.chatSessionStore, b.chatCatalog(), opts, taskTools)
|
||||
}
|
||||
|
||||
func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
|
||||
@@ -98,9 +101,10 @@ func gatewayServerOpts(listen string, id *rpctls.Identity, principals access.Pri
|
||||
}, nil
|
||||
}
|
||||
|
||||
func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, catalog gatewayrpc.Catalog, opts []grpc.ServerOption) error {
|
||||
func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, catalog gatewayrpc.Catalog, opts []grpc.ServerOption, taskTools pb.TaskToolsServiceServer) error {
|
||||
server := grpc.NewServer(append(servicekit.ServerLimits(), opts...)...)
|
||||
gatewayrpc.RegisterServer(server, contract, sessions, catalog)
|
||||
pb.RegisterTaskToolsServiceServer(server, taskTools)
|
||||
// Serve's bounded drain returns before the session store and tools close.
|
||||
return servicekit.Serve(ctx, server, listener)
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
func executeTaskTool(ctx context.Context, entry tools.ToolEntry, input json.RawMessage, limit int) (result *mcp.CallToolResult, err error) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
result = nil
|
||||
err = fmt.Errorf("tool execution failed")
|
||||
}
|
||||
}()
|
||||
if !entry.Available() {
|
||||
return nil, fmt.Errorf("tool is unavailable")
|
||||
}
|
||||
schema, err := json.Marshal(taskToolSchema(entry))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("tool schema unavailable")
|
||||
}
|
||||
if len(input) == 0 {
|
||||
input = json.RawMessage(`{}`)
|
||||
}
|
||||
if reason, ok := agentexec.ValidateCaptureArgs(agentrun.CaptureTool{Name: entry.Name, Parameters: schema}, input); !ok {
|
||||
return nil, fmt.Errorf("%s", reason)
|
||||
}
|
||||
var args map[string]any
|
||||
if err := json.Unmarshal(input, &args); err != nil {
|
||||
return nil, fmt.Errorf("arguments must be an object")
|
||||
}
|
||||
out, err := entry.Handler(ctx, args)
|
||||
if err != nil {
|
||||
return &mcp.CallToolResult{IsError: true, Content: []mcp.Content{&mcp.TextContent{Text: "tool execution failed"}}}, nil
|
||||
}
|
||||
if entry.MaxResultSizeChars != 0 {
|
||||
limit = entry.MaxResultSizeChars
|
||||
}
|
||||
if media, ok := out.(tools.MultimodalResult); ok {
|
||||
return taskToolMedia(entry.Name, media, limit)
|
||||
}
|
||||
raw, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode tool result")
|
||||
}
|
||||
return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: tools.CapPayload(entry.Name, string(raw), limit, "")}}}, nil
|
||||
}
|
||||
|
||||
func taskToolMedia(name string, media tools.MultimodalResult, limit int) (*mcp.CallToolResult, error) {
|
||||
result := &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: tools.CapPayload(name, media.Summary, limit, "")}}}
|
||||
root, err := os.OpenRoot(media.SubdirHint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("tool media unavailable")
|
||||
}
|
||||
defer root.Close()
|
||||
for _, file := range media.Files {
|
||||
relative, err := filepath.Rel(media.SubdirHint, file)
|
||||
if err != nil || !filepath.IsLocal(relative) {
|
||||
return nil, fmt.Errorf("tool media outside provider directory")
|
||||
}
|
||||
raw, err := root.ReadFile(relative)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read tool media")
|
||||
}
|
||||
mimeType := mime.TypeByExtension(filepath.Ext(file))
|
||||
switch mimeType {
|
||||
case "image/png", "image/jpeg", "image/gif", "image/webp":
|
||||
result.Content = append(result.Content, &mcp.ImageContent{Data: raw, MIMEType: mimeType})
|
||||
case "audio/wav", "audio/mpeg":
|
||||
result.Content = append(result.Content, &mcp.AudioContent{Data: raw, MIMEType: mimeType})
|
||||
default:
|
||||
result.Content = append(result.Content, &mcp.EmbeddedResource{Resource: &mcp.ResourceContents{URI: "archie-media:///" + filepath.Base(file), MIMEType: mimeType, Blob: raw}})
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func taskToolSchema(entry tools.ToolEntry) tools.JSONSchema {
|
||||
schema := entry.ResolvedSchema()
|
||||
if schema == nil {
|
||||
return tools.JSONSchema{"type": "object", "properties": map[string]any{}}
|
||||
}
|
||||
return schema
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
func TestTaskToolResults(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, input string
|
||||
output any
|
||||
limit int
|
||||
badPath bool
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "text limit", input: `{"value":"valid"}`, output: strings.Repeat("a", 200), limit: 40},
|
||||
{name: "schema enforced", input: `{"value":true}`, wantErr: true},
|
||||
{name: "media", input: `{"value":"valid"}`},
|
||||
{name: "media cannot escape", input: `{"value":"valid"}`, badPath: true, wantErr: true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
file := filepath.Join(root, "image.png")
|
||||
if tt.badPath {
|
||||
file = filepath.Join(t.TempDir(), "outside.png")
|
||||
}
|
||||
if err := os.WriteFile(file, []byte("image-data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output := tt.output
|
||||
if output == nil {
|
||||
output = tools.MultimodalResult{Summary: "image", SubdirHint: root, Files: []string{file}}
|
||||
}
|
||||
called := false
|
||||
entry := tools.ToolEntry{Name: "tool", Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"value": map[string]any{"type": "string"}}, "required": []string{"value"}}, Handler: func(context.Context, map[string]any) (any, error) { called = true; return output, nil }}
|
||||
result, err := executeTaskTool(t.Context(), entry, json.RawMessage(tt.input), tt.limit)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("error %v, want error %v", err, tt.wantErr)
|
||||
}
|
||||
if tt.name == "schema enforced" && called {
|
||||
t.Fatal("invalid arguments executed")
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(raw), root) {
|
||||
t.Fatal("host media path escaped")
|
||||
}
|
||||
if tt.name == "media" {
|
||||
image, ok := result.Content[1].(*mcp.ImageContent)
|
||||
if !ok || string(image.Data) != "image-data" {
|
||||
t.Fatalf("media result %s", raw)
|
||||
}
|
||||
}
|
||||
text, ok := result.Content[0].(*mcp.TextContent)
|
||||
if !ok {
|
||||
t.Fatal("missing text content")
|
||||
}
|
||||
if tt.limit > 0 && strings.Contains(text.Text, strings.Repeat("a", 200)) {
|
||||
t.Fatal("tool result limit was ignored")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskmcp"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
type taskToolSession struct {
|
||||
mu sync.Mutex
|
||||
lifetime context.Context //nolint:containedctx // Providers belong to the run, not one RPC.
|
||||
providers *taskmcp.Set
|
||||
configHash [32]byte
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
type taskToolAuthority interface {
|
||||
TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error)
|
||||
PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error)
|
||||
GetInstalled(context.Context, string) (storepkg.Installed, error)
|
||||
TaskModels(context.Context, int64) (config.OrgModels, error)
|
||||
RecordUsage(context.Context, usage.Record) error
|
||||
}
|
||||
|
||||
type taskToolsServer struct {
|
||||
pb.UnimplementedTaskToolsServiceServer
|
||||
gateway *server
|
||||
authority taskToolAuthority
|
||||
configFor func(context.Context) (config.Config, error)
|
||||
mu sync.Mutex
|
||||
sessions map[[32]byte]*taskToolSession
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) authorize(ctx context.Context, credential string) (context.Context, *task.Task, config.Config, []string, error) {
|
||||
if access.SourceFromContext(ctx) != "archied" {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task tools require the daemon")
|
||||
}
|
||||
if credential == "" {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.Unauthenticated, "task grant required")
|
||||
}
|
||||
t, principal, err := s.authority.TaskPrincipalForCredential(ctx, credential)
|
||||
if err != nil {
|
||||
s.close(ctx, credential)
|
||||
code := codes.Unauthenticated
|
||||
if errors.Is(err, storecontract.ErrUnavailable) {
|
||||
code = codes.Unavailable
|
||||
}
|
||||
return nil, nil, config.Config{}, nil, status.Error(code, "task grant unavailable")
|
||||
}
|
||||
if t.Status != "running" {
|
||||
s.close(ctx, credential)
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task is not running")
|
||||
}
|
||||
p, err := s.authority.PrincipalFor(ctx, principal)
|
||||
if err != nil || string(p.Org) != t.OrgOrDefault() {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task identity unavailable")
|
||||
}
|
||||
ctx = org.WithScope(org.WithOrg(access.WithPrincipal(access.WithActor(ctx, string(principal)), p), p.Org), p.Org)
|
||||
iface, err := task.ParseWorkflowInterface(t.WorkflowDefinitionYAML)
|
||||
if err != nil {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task workflow unavailable")
|
||||
}
|
||||
cfg, err := s.configFor(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.Unavailable, "task tool settings unavailable")
|
||||
}
|
||||
profile, err := cfg.Containers.Profile(iface.Profile)
|
||||
if err != nil || profile.Kit == "" {
|
||||
return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task has no Kit profile")
|
||||
}
|
||||
packaged := workflow.PackageOf(t.WorkflowDefinitionYAML)
|
||||
var accepted []string
|
||||
if packaged != "" {
|
||||
installed, err := s.authority.GetInstalled(ctx, packaged)
|
||||
if err == nil && installed.AcceptedAuthority != nil {
|
||||
accepted = installed.AcceptedAuthority.Tools
|
||||
}
|
||||
}
|
||||
return ctx, t, cfg, storepkg.AllowedTools(profile.Tools, packaged != "", accepted), nil
|
||||
}
|
||||
|
||||
func taskToolAllowed(entry tools.ToolEntry, allowed []string) bool {
|
||||
return entry.Toolset != "workspace" && entry.Name != "send_file" && !entry.Classification.IsApprovalRequired() && (allowed == nil || slices.Contains(allowed, entry.Name))
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) entries(ctx context.Context, t *task.Task, cfg config.Config, credential string, allowed []string) ([]tools.ToolEntry, error) {
|
||||
entries := []tools.ToolEntry{}
|
||||
if allowed != nil && len(allowed) == 0 {
|
||||
s.close(ctx, credential)
|
||||
return entries, nil
|
||||
}
|
||||
for _, entry := range s.gateway.toolReg.All() {
|
||||
// Task MCP providers have their own sampling identity and lifecycle.
|
||||
if entry.Toolset != "mcp" && taskToolAllowed(entry, allowed) {
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
}
|
||||
providers, err := s.providers(ctx, t, cfg, credential)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if providers != nil {
|
||||
for _, entry := range providers.Registry.All() {
|
||||
if taskToolAllowed(entry, allowed) {
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) providers(ctx context.Context, t *task.Task, cfg config.Config, credential string) (*taskmcp.Set, error) {
|
||||
if len(cfg.Tools.MCPServers) == 0 {
|
||||
s.close(ctx, credential)
|
||||
return nil, nil
|
||||
}
|
||||
models, err := s.authority.TaskModels(ctx, t.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("task sampling model: %w", err)
|
||||
}
|
||||
own := cfg.Providers
|
||||
cfg.Providers = maps.Clone(s.gateway.cfgHolder.Get().Providers)
|
||||
cfg = cfg.WithOrgModels(models)
|
||||
keys := map[string]string{}
|
||||
for name := range models.Providers {
|
||||
provider, ok := own[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("task provider settings unavailable")
|
||||
}
|
||||
if provider.APIKey != (config.SecretRef{}) {
|
||||
value, err := s.gateway.secrets.Resolve(provider.APIKey)
|
||||
if err != nil || value == "" {
|
||||
return nil, fmt.Errorf("task provider credential unavailable")
|
||||
}
|
||||
keys[name] = value
|
||||
}
|
||||
}
|
||||
encoded, err := json.Marshal(struct {
|
||||
Servers []config.MCPServer
|
||||
Models map[string]string
|
||||
Providers map[string]config.Provider
|
||||
Keys map[string]string
|
||||
Limits map[string]config.ModelLimits
|
||||
}{cfg.Tools.MCPServers, cfg.Models, cfg.Providers, keys, cfg.ModelLimits})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.loadProviders(ctx, t, cfg, credential, keys, sha256.Sum256(encoded))
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) loadProviders(ctx context.Context, t *task.Task, cfg config.Config, credential string, keys map[string]string, hash [32]byte) (*taskmcp.Set, error) {
|
||||
key := sha256.Sum256([]byte(credential))
|
||||
s.mu.Lock()
|
||||
current := s.sessions[key]
|
||||
if current == nil || current.lifetime.Err() != nil {
|
||||
lifetime, cancel := context.WithCancel(context.WithoutCancel(ctx))
|
||||
current = &taskToolSession{lifetime: lifetime, cancel: cancel}
|
||||
s.sessions[key] = current
|
||||
}
|
||||
s.mu.Unlock()
|
||||
current.mu.Lock()
|
||||
defer current.mu.Unlock()
|
||||
if err := current.lifetime.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if current.providers != nil {
|
||||
if current.configHash == hash {
|
||||
return current.providers, nil
|
||||
}
|
||||
current.providers.Close(context.WithoutCancel(ctx), s.gateway.log)
|
||||
current.providers = nil
|
||||
}
|
||||
// Cancel an unfinished startup with its RPC; a ready provider stays run-scoped.
|
||||
stop := context.AfterFunc(ctx, current.cancel)
|
||||
defer stop()
|
||||
llm := modelloop.NewRuntimeWithKeys(agentexec.ProvidersFromConfig(cfg.Providers), cfg.ModelLimits, keys)
|
||||
cacheDir := ""
|
||||
if cfg.WorkDir != "" {
|
||||
cacheDir = filepath.Join(cfg.WorkDir, "mcp-npm-cache")
|
||||
}
|
||||
providers, err := taskmcp.Start(current.lifetime, cfg.Tools.MCPServers, s.taskSampling(llm, cfg, t, credential), s.gateway.log, cacheDir) //nolint:contextcheck // The run owns provider process lifetime.
|
||||
if err != nil || ctx.Err() != nil {
|
||||
current.cancel()
|
||||
if providers != nil {
|
||||
providers.Close(context.WithoutCancel(ctx), s.gateway.log)
|
||||
}
|
||||
return nil, errors.Join(err, ctx.Err())
|
||||
}
|
||||
current.providers, current.configHash = providers, hash
|
||||
return providers, nil
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) ListTaskTools(ctx context.Context, req *pb.ListTaskToolsRequest) (*pb.ListTaskToolsResponse, error) {
|
||||
ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "task tool providers unavailable")
|
||||
}
|
||||
set, err := modelloop.BuildToolSetFrom(entries, modelloop.ToolSetOptions{})
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "task tool discovery failed")
|
||||
}
|
||||
result := []*mcp.Tool{}
|
||||
for _, entry := range entries {
|
||||
tool, ok := set[entry.Name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
result = append(result, &mcp.Tool{Name: tool.Name, Description: tool.Description, InputSchema: tool.Parameters})
|
||||
}
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "encode task tools")
|
||||
}
|
||||
return &pb.ListTaskToolsResponse{ToolsJson: data}, nil
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) CallTaskTool(ctx context.Context, req *pb.CallTaskToolRequest) (*pb.CallTaskToolResponse, error) {
|
||||
ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "task tool providers unavailable")
|
||||
}
|
||||
entry, ok := findTaskTool(entries, req.Name)
|
||||
if !ok {
|
||||
return nil, status.Error(codes.PermissionDenied, "tool is not allowed for this task")
|
||||
}
|
||||
result, err := executeTaskTool(ctx, entry, req.ArgumentsJson, cfg.Tools.Policy.MaxResultChars)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "task tool call failed: "+err.Error())
|
||||
}
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "encode task tool result")
|
||||
}
|
||||
return &pb.CallTaskToolResponse{ResultJson: data}, nil
|
||||
}
|
||||
|
||||
func findTaskTool(entries []tools.ToolEntry, name string) (tools.ToolEntry, bool) {
|
||||
for _, entry := range entries {
|
||||
if entry.Name == name {
|
||||
return entry, true
|
||||
}
|
||||
}
|
||||
return tools.ToolEntry{}, false
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) CloseTaskTools(ctx context.Context, req *pb.CloseTaskToolsRequest) (*pb.CloseTaskToolsResponse, error) {
|
||||
if access.SourceFromContext(ctx) != "archied" {
|
||||
return nil, status.Error(codes.PermissionDenied, "task tools require the daemon")
|
||||
}
|
||||
s.close(ctx, req.RunCredential)
|
||||
return &pb.CloseTaskToolsResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) close(parent context.Context, credential string) {
|
||||
key := sha256.Sum256([]byte(credential))
|
||||
s.mu.Lock()
|
||||
current := s.sessions[key]
|
||||
delete(s.sessions, key)
|
||||
s.mu.Unlock()
|
||||
if current == nil {
|
||||
return
|
||||
}
|
||||
current.cancel()
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second)
|
||||
defer cancel()
|
||||
current.mu.Lock()
|
||||
defer current.mu.Unlock()
|
||||
if current.providers != nil {
|
||||
current.providers.Close(ctx, s.gateway.log)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) Close(parent context.Context) {
|
||||
s.mu.Lock()
|
||||
sessions := s.sessions
|
||||
s.sessions = make(map[[32]byte]*taskToolSession)
|
||||
s.mu.Unlock()
|
||||
for _, session := range sessions {
|
||||
session.cancel()
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second)
|
||||
session.mu.Lock()
|
||||
if session.providers != nil {
|
||||
session.providers.Close(ctx, s.gateway.log)
|
||||
}
|
||||
session.mu.Unlock()
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *server) taskToolsConfig(ctx context.Context) (config.Config, error) {
|
||||
base := b.cfgHolder.Get()
|
||||
// These documents belong to the task's org; absence must not inherit
|
||||
// the instance's MCP credentials or agent profiles.
|
||||
base.Tools.MCPServers = nil
|
||||
base.Containers.Profiles = nil
|
||||
base.Providers = nil
|
||||
cfg, _, err := b.controlPlane.RuntimeConfig(ctx, base)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
func (s *taskToolsServer) taskSampling(llm *runtime.Runtime, cfg config.Config, t *task.Task, credential string) protocol.SamplingHandler {
|
||||
sample := taskmcp.SamplingHandler(llm, cfg.ForTask(), taskmcp.UsageRecorder(s.authority, t, s.gateway.log))
|
||||
return func(ctx context.Context, request protocol.SamplingRequest) (protocol.SamplingResult, error) {
|
||||
live, _, err := s.authority.TaskPrincipalForCredential(ctx, credential)
|
||||
if err != nil || live.ID != t.ID || live.Status != task.StatusRunning || live.OrgOrDefault() != t.OrgOrDefault() {
|
||||
return protocol.SamplingResult{}, errors.New("task grant no longer authorizes sampling")
|
||||
}
|
||||
return sample(ctx, request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
type taskToolGrants struct {
|
||||
taskToolAuthority
|
||||
task task.Task
|
||||
principal access.Principal
|
||||
accepted *storepkg.Authority
|
||||
revoked bool
|
||||
recorded []usage.Record
|
||||
}
|
||||
|
||||
func (a *taskToolGrants) TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error) {
|
||||
if a.revoked {
|
||||
return nil, "", errors.New("revoked")
|
||||
}
|
||||
return &a.task, a.principal.IdentityID, nil
|
||||
}
|
||||
|
||||
func (a *taskToolGrants) PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error) {
|
||||
return a.principal, nil
|
||||
}
|
||||
|
||||
func (a *taskToolGrants) RecordUsage(_ context.Context, value usage.Record) error {
|
||||
a.recorded = append(a.recorded, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *taskToolGrants) GetInstalled(context.Context, string) (storepkg.Installed, error) {
|
||||
return storepkg.Installed{AcceptedAuthority: a.accepted}, nil
|
||||
}
|
||||
|
||||
func TestTaskToolAuthorization(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, caller, token, tool string
|
||||
profile []string
|
||||
packaged, revoked bool
|
||||
accepted *storepkg.Authority
|
||||
principalOrg org.OrgID
|
||||
status string
|
||||
want codes.Code
|
||||
}{
|
||||
{name: "allowed", tool: "fetch", want: codes.OK},
|
||||
{name: "profile denial", tool: "fetch", profile: []string{"other"}, want: codes.PermissionDenied},
|
||||
{name: "unaccepted package", tool: "fetch", packaged: true, want: codes.PermissionDenied},
|
||||
{name: "accepted package", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}},
|
||||
{name: "package intersection", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}, profile: []string{"other"}, want: codes.PermissionDenied},
|
||||
{name: "workspace never served", tool: "shell", want: codes.PermissionDenied},
|
||||
{name: "chat files never served", tool: "send_file", want: codes.PermissionDenied},
|
||||
{name: "approval unavailable", tool: "danger", want: codes.PermissionDenied},
|
||||
{name: "revoked", tool: "fetch", revoked: true, want: codes.Unauthenticated},
|
||||
{name: "not running", tool: "fetch", status: "completed", want: codes.PermissionDenied},
|
||||
{name: "other org principal", tool: "fetch", principalOrg: "victim", want: codes.PermissionDenied},
|
||||
{name: "non daemon", caller: "archie-ui", tool: "fetch", want: codes.PermissionDenied},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}, accepted: tt.accepted, revoked: tt.revoked}
|
||||
if tt.packaged {
|
||||
authority.task.WorkflowDefinitionYAML += "package: sample\n"
|
||||
}
|
||||
if tt.status != "" {
|
||||
authority.task.Status = tt.status
|
||||
}
|
||||
if tt.principalOrg != "" {
|
||||
authority.principal.Org = tt.principalOrg
|
||||
}
|
||||
called := 0
|
||||
reg := tools.NewRegistry()
|
||||
for _, entry := range []tools.ToolEntry{
|
||||
{Name: "fetch", Toolset: "web"}, {Name: "shell", Toolset: "workspace"}, {Name: "send_file", Toolset: "media"}, {Name: "danger", Classification: tools.RequiresApproval},
|
||||
} {
|
||||
entry.Schema = tools.JSONSchema{"type": "object"}
|
||||
entry.Handler = func(ctx context.Context, _ map[string]any) (any, error) {
|
||||
called++
|
||||
p, _ := access.PrincipalFromContext(ctx)
|
||||
if p.IdentityID != "bot" || org.OrgFromContext(ctx) != "acme" {
|
||||
t.Fatal("tool executed with caller-selected authority")
|
||||
}
|
||||
return "result", nil
|
||||
}
|
||||
if err := reg.Register(entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "image@sha256:pinned", Tools: tt.profile}}}}
|
||||
s := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)}
|
||||
caller := tt.caller
|
||||
if caller == "" {
|
||||
caller = "archied"
|
||||
}
|
||||
ctx := access.WithSource(t.Context(), caller)
|
||||
result, err := s.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: "grant"})
|
||||
if err == nil {
|
||||
var listed []*mcp.Tool
|
||||
if err := json.Unmarshal(result.ToolsJson, &listed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tool := range listed {
|
||||
if slices.Contains([]string{"shell", "send_file", "danger"}, tool.Name) {
|
||||
t.Fatalf("unsafe tool listed: %s", tool.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err = s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: tt.tool, ArgumentsJson: []byte(`{}`)})
|
||||
if status.Code(err) != tt.want {
|
||||
t.Fatalf("error %v, want %v", err, tt.want)
|
||||
}
|
||||
if tt.want == codes.OK && called != 1 {
|
||||
t.Fatalf("calls %d, want 1", called)
|
||||
}
|
||||
if tt.want != codes.OK && called != 0 {
|
||||
t.Fatal("denied request executed")
|
||||
}
|
||||
// Authorization must be checked again after a successful discovery.
|
||||
authority.revoked = true
|
||||
if _, err := s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: "fetch", ArgumentsJson: []byte(`{}`)}); err == nil {
|
||||
t.Fatal("revoked grant remained usable")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskToolGatewayThroughProxy(t *testing.T) {
|
||||
authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}}
|
||||
reg := tools.NewRegistry()
|
||||
calls := 0
|
||||
blocked := make(chan struct{})
|
||||
if err := reg.Register(tools.ToolEntry{Name: "echo", Toolset: "web", Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"value": map[string]any{"type": "string"}}, "required": []string{"value"}}, Handler: func(ctx context.Context, args map[string]any) (any, error) {
|
||||
calls++
|
||||
if org.OrgFromContext(ctx) != "acme" {
|
||||
t.Error("lost task org")
|
||||
}
|
||||
if args["value"] == "block" {
|
||||
close(blocked)
|
||||
<-ctx.Done()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return args["value"], nil
|
||||
}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "pinned", Tools: []string{"echo"}}}}}
|
||||
service := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)}
|
||||
defer service.Close(t.Context())
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
rpc := grpc.NewServer(grpc.ChainUnaryInterceptor((rpcidentity.Callers{}).Unary()))
|
||||
pb.RegisterTaskToolsServiceServer(rpc, service)
|
||||
go func() { _ = rpc.Serve(listener) }()
|
||||
defer rpc.Stop()
|
||||
conn, err := grpc.NewClient("passthrough:///gateway", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return listener.Dial() }), grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor("archied", "")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
proxy := egress.NewProxy(nil, egress.ProxyOptions{TaskTools: tasktools.NewClient(conn)})
|
||||
session, err := proxy.Register(egress.SessionOptions{Token: "proxy-password", RunCredential: "grant", Org: "acme"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer proxy.Revoke(t.Context(), session.Token())
|
||||
session.EnterRuntime()
|
||||
httpServer := httptest.NewServer(proxy)
|
||||
defer httpServer.Close()
|
||||
proxyURL, err := url.Parse(httpServer.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
proxyURL.User = url.UserPassword("archie", session.Token())
|
||||
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
|
||||
defer transport.CloseIdleConnections()
|
||||
client := mcp.NewClient(&mcp.Implementation{Name: "test-harness", Version: "1"}, nil)
|
||||
clientSession, err := client.Connect(t.Context(), &mcp.StreamableClientTransport{Endpoint: tasktools.Endpoint, HTTPClient: &http.Client{Transport: taskSentinelTransport{base: transport}}, DisableStandaloneSSE: true, MaxRetries: -1}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer clientSession.Close()
|
||||
listed, err := clientSession.ListTools(t.Context(), nil)
|
||||
if err != nil || len(listed.Tools) != 1 || listed.Tools[0].Name != "echo" {
|
||||
t.Fatalf("discovery %v %v", listed, err)
|
||||
}
|
||||
result, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "hello"}})
|
||||
if err != nil || result.IsError || calls != 1 {
|
||||
t.Fatalf("call %v %v calls %d", result, err, calls)
|
||||
}
|
||||
if _, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "forbidden", Arguments: map[string]any{}}); err == nil {
|
||||
t.Fatal("unknown tool accepted")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatal("unknown tool executed")
|
||||
}
|
||||
samples := 0
|
||||
model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
samples++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"sample","object":"chat.completion","model":"task","choices":[{"index":0,"message":{"role":"assistant","content":"sampled"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`))
|
||||
}))
|
||||
defer model.Close()
|
||||
llm := modelloop.NewRuntime(map[string]agentrun.Provider{"fixture": {Class: "openai", BaseURL: model.URL + "/v1"}}, nil)
|
||||
sample := service.taskSampling(llm, config.Config{Models: map[string]string{"default": "fixture/task"}}, &authority.task, "grant")
|
||||
if _, err := sample(t.Context(), protocol.SamplingRequest{MaxTokens: 5, Messages: []protocol.SamplingMessage{{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "sample"}}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if samples != 1 || len(authority.recorded) != 1 || authority.recorded[0].TaskID != 7 || authority.recorded[0].Source != usage.SourceTask {
|
||||
t.Fatal("sampling lost task attribution")
|
||||
}
|
||||
authority.revoked = true
|
||||
result, err = clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "again"}})
|
||||
if err == nil && !result.IsError {
|
||||
t.Fatal("revoked grant executed")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatal("revoked tool executed")
|
||||
}
|
||||
if _, err := sample(t.Context(), protocol.SamplingRequest{}); err == nil || samples != 1 {
|
||||
t.Fatal("revoked grant sampled a model")
|
||||
}
|
||||
authority.revoked = false
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
finished := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := clientSession.CallTool(ctx, &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "block"}})
|
||||
finished <- err
|
||||
}()
|
||||
select {
|
||||
case <-blocked:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("tool call did not start")
|
||||
}
|
||||
proxy.Revoke(ctx, session.Token())
|
||||
select {
|
||||
case <-finished:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("proxy revocation did not cancel the active call")
|
||||
}
|
||||
}
|
||||
|
||||
type taskSentinelTransport struct{ base http.RoundTripper }
|
||||
|
||||
func (t taskSentinelTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
r = r.Clone(r.Context())
|
||||
r.Header.Set("Authorization", "Bearer "+tasktools.Sentinel)
|
||||
return t.base.RoundTrip(r)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -12,12 +11,12 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskmcp"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
minimaxtool "github.com/samcharles93/archie-core/internal/tools/minimax"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
||||
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
||||
"github.com/samcharles93/archie-core/internal/tools/sendfile"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
)
|
||||
@@ -168,80 +167,12 @@ func isWithin(base, target string) bool {
|
||||
}
|
||||
|
||||
func configuredMCPProvider(server config.MCPServer, workDir string, sampling mcp.SamplingHandler) (toolprovider.Engine, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("MCP server name is required")
|
||||
cacheDir := ""
|
||||
if workDir != "" {
|
||||
cacheDir = filepath.Join(workDir, "mcp-npm-cache")
|
||||
}
|
||||
transportType := strings.ToLower(strings.TrimSpace(server.Transport))
|
||||
if transportType == "" {
|
||||
transportType = "stdio"
|
||||
}
|
||||
|
||||
samplingOption := mcptoolprovider.WithSamplingHandler(sampling)
|
||||
|
||||
switch transportType {
|
||||
case "stdio":
|
||||
command := strings.TrimSpace(server.Command)
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("MCP stdio server %q requires a command", name)
|
||||
}
|
||||
transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{
|
||||
Command: command,
|
||||
Args: append([]string(nil), server.Args...),
|
||||
Dir: server.WorkDir,
|
||||
Env: npmCacheServerEnv(command, workDir),
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "http", "streamablehttp":
|
||||
url := strings.TrimSpace(server.URL)
|
||||
if url == "" {
|
||||
return nil, fmt.Errorf("MCP http server %q requires a url", name)
|
||||
}
|
||||
transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{
|
||||
Endpoint: url,
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "sse":
|
||||
sseEndpoint := strings.TrimSpace(server.SSEEndpoint)
|
||||
if sseEndpoint == "" {
|
||||
return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name)
|
||||
}
|
||||
transport := mcp.NewSSETransport(mcp.SSETransportConfig{
|
||||
SSEEndpoint: sseEndpoint,
|
||||
MessageEndpoint: strings.TrimSpace(server.MessageEndpoint),
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("MCP transport %q is not supported", transportType)
|
||||
}
|
||||
}
|
||||
|
||||
// npmCacheServerEnv returns environment variables that make an
|
||||
// npx-launched MCP server reuse a persistent package cache across Gateway
|
||||
// restarts, instead of npm re-resolving and re-downloading the package
|
||||
// from the registry every time the Gateway starts. Rooted under the
|
||||
// work dir so it persists regardless of whether the Gateway
|
||||
// itself runs in an ephemeral container. See mcp.NpmCacheEnv for the
|
||||
// command-matching and env var details shared with archie-agent's
|
||||
// per-task equivalent (internal/app/agentworker/mcp_providers.go), which
|
||||
// points at a mounted cache volume instead of a work-dir subdirectory.
|
||||
//
|
||||
// workDir is unconditionally defaulted before registerTools runs
|
||||
// (internal/infrastructure/configuration's applyGeneralDefaults), but
|
||||
// filepath.Join silently accepts an empty string and would then put the
|
||||
// cache in the process's current working directory instead of its
|
||||
// persistent data directory -- guard it explicitly rather than trust that
|
||||
// invariant here too.
|
||||
func npmCacheServerEnv(command, workDir string) []string {
|
||||
if workDir == "" {
|
||||
return nil
|
||||
}
|
||||
return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache"))
|
||||
provider, _, err := taskmcp.NewProvider(server, sampling, cacheDir)
|
||||
return provider, err
|
||||
}
|
||||
|
||||
func shutdownCapabilityHost(ctx context.Context, capabilityHost *plugin.Host, log *slog.Logger) func() {
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc (unknown)
|
||||
// source: gateway/v1/task_tools.proto
|
||||
|
||||
package gatewayv1
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type ListTaskToolsRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsRequest) Reset() {
|
||||
*x = ListTaskToolsRequest{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ListTaskToolsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *ListTaskToolsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ListTaskToolsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*ListTaskToolsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsRequest) GetRunCredential() string {
|
||||
if x != nil {
|
||||
return x.RunCredential
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ListTaskToolsResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
ToolsJson []byte `protobuf:"bytes,1,opt,name=tools_json,json=toolsJson,proto3" json:"tools_json,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsResponse) Reset() {
|
||||
*x = ListTaskToolsResponse{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ListTaskToolsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *ListTaskToolsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ListTaskToolsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*ListTaskToolsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *ListTaskToolsResponse) GetToolsJson() []byte {
|
||||
if x != nil {
|
||||
return x.ToolsJson
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type CallTaskToolRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
||||
ArgumentsJson []byte `protobuf:"bytes,3,opt,name=arguments_json,json=argumentsJson,proto3" json:"arguments_json,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CallTaskToolRequest) Reset() {
|
||||
*x = CallTaskToolRequest{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CallTaskToolRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CallTaskToolRequest) ProtoMessage() {}
|
||||
|
||||
func (x *CallTaskToolRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CallTaskToolRequest.ProtoReflect.Descriptor instead.
|
||||
func (*CallTaskToolRequest) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *CallTaskToolRequest) GetRunCredential() string {
|
||||
if x != nil {
|
||||
return x.RunCredential
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CallTaskToolRequest) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CallTaskToolRequest) GetArgumentsJson() []byte {
|
||||
if x != nil {
|
||||
return x.ArgumentsJson
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type CallTaskToolResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
ResultJson []byte `protobuf:"bytes,1,opt,name=result_json,json=resultJson,proto3" json:"result_json,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CallTaskToolResponse) Reset() {
|
||||
*x = CallTaskToolResponse{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CallTaskToolResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CallTaskToolResponse) ProtoMessage() {}
|
||||
|
||||
func (x *CallTaskToolResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CallTaskToolResponse.ProtoReflect.Descriptor instead.
|
||||
func (*CallTaskToolResponse) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *CallTaskToolResponse) GetResultJson() []byte {
|
||||
if x != nil {
|
||||
return x.ResultJson
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type CloseTaskToolsRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CloseTaskToolsRequest) Reset() {
|
||||
*x = CloseTaskToolsRequest{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CloseTaskToolsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CloseTaskToolsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *CloseTaskToolsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CloseTaskToolsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*CloseTaskToolsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *CloseTaskToolsRequest) GetRunCredential() string {
|
||||
if x != nil {
|
||||
return x.RunCredential
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CloseTaskToolsResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CloseTaskToolsResponse) Reset() {
|
||||
*x = CloseTaskToolsResponse{}
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *CloseTaskToolsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*CloseTaskToolsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *CloseTaskToolsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_gateway_v1_task_tools_proto_msgTypes[5]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use CloseTaskToolsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*CloseTaskToolsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
var File_gateway_v1_task_tools_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_gateway_v1_task_tools_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x1bgateway/v1/task_tools.proto\x12\n" +
|
||||
"gateway.v1\"=\n" +
|
||||
"\x14ListTaskToolsRequest\x12%\n" +
|
||||
"\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\"6\n" +
|
||||
"\x15ListTaskToolsResponse\x12\x1d\n" +
|
||||
"\n" +
|
||||
"tools_json\x18\x01 \x01(\fR\ttoolsJson\"w\n" +
|
||||
"\x13CallTaskToolRequest\x12%\n" +
|
||||
"\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\x12\x12\n" +
|
||||
"\x04name\x18\x02 \x01(\tR\x04name\x12%\n" +
|
||||
"\x0earguments_json\x18\x03 \x01(\fR\rargumentsJson\"7\n" +
|
||||
"\x14CallTaskToolResponse\x12\x1f\n" +
|
||||
"\vresult_json\x18\x01 \x01(\fR\n" +
|
||||
"resultJson\">\n" +
|
||||
"\x15CloseTaskToolsRequest\x12%\n" +
|
||||
"\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\"\x18\n" +
|
||||
"\x16CloseTaskToolsResponse2\x94\x02\n" +
|
||||
"\x10TaskToolsService\x12T\n" +
|
||||
"\rListTaskTools\x12 .gateway.v1.ListTaskToolsRequest\x1a!.gateway.v1.ListTaskToolsResponse\x12Q\n" +
|
||||
"\fCallTaskTool\x12\x1f.gateway.v1.CallTaskToolRequest\x1a .gateway.v1.CallTaskToolResponse\x12W\n" +
|
||||
"\x0eCloseTaskTools\x12!.gateway.v1.CloseTaskToolsRequest\x1a\".gateway.v1.CloseTaskToolsResponseB\xb6\x01\n" +
|
||||
"\x0ecom.gateway.v1B\x0eTaskToolsProtoP\x01ZKgithub.com/samcharles93/archie-core/internal/contracts/gateway/v1;gatewayv1\xa2\x02\x03GXX\xaa\x02\n" +
|
||||
"Gateway.V1\xca\x02\n" +
|
||||
"Gateway\\V1\xe2\x02\x16Gateway\\V1\\GPBMetadata\xea\x02\vGateway::V1b\x06proto3"
|
||||
|
||||
var (
|
||||
file_gateway_v1_task_tools_proto_rawDescOnce sync.Once
|
||||
file_gateway_v1_task_tools_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_gateway_v1_task_tools_proto_rawDescGZIP() []byte {
|
||||
file_gateway_v1_task_tools_proto_rawDescOnce.Do(func() {
|
||||
file_gateway_v1_task_tools_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_gateway_v1_task_tools_proto_rawDesc), len(file_gateway_v1_task_tools_proto_rawDesc)))
|
||||
})
|
||||
return file_gateway_v1_task_tools_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_gateway_v1_task_tools_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_gateway_v1_task_tools_proto_goTypes = []any{
|
||||
(*ListTaskToolsRequest)(nil), // 0: gateway.v1.ListTaskToolsRequest
|
||||
(*ListTaskToolsResponse)(nil), // 1: gateway.v1.ListTaskToolsResponse
|
||||
(*CallTaskToolRequest)(nil), // 2: gateway.v1.CallTaskToolRequest
|
||||
(*CallTaskToolResponse)(nil), // 3: gateway.v1.CallTaskToolResponse
|
||||
(*CloseTaskToolsRequest)(nil), // 4: gateway.v1.CloseTaskToolsRequest
|
||||
(*CloseTaskToolsResponse)(nil), // 5: gateway.v1.CloseTaskToolsResponse
|
||||
}
|
||||
var file_gateway_v1_task_tools_proto_depIdxs = []int32{
|
||||
0, // 0: gateway.v1.TaskToolsService.ListTaskTools:input_type -> gateway.v1.ListTaskToolsRequest
|
||||
2, // 1: gateway.v1.TaskToolsService.CallTaskTool:input_type -> gateway.v1.CallTaskToolRequest
|
||||
4, // 2: gateway.v1.TaskToolsService.CloseTaskTools:input_type -> gateway.v1.CloseTaskToolsRequest
|
||||
1, // 3: gateway.v1.TaskToolsService.ListTaskTools:output_type -> gateway.v1.ListTaskToolsResponse
|
||||
3, // 4: gateway.v1.TaskToolsService.CallTaskTool:output_type -> gateway.v1.CallTaskToolResponse
|
||||
5, // 5: gateway.v1.TaskToolsService.CloseTaskTools:output_type -> gateway.v1.CloseTaskToolsResponse
|
||||
3, // [3:6] is the sub-list for method output_type
|
||||
0, // [0:3] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_gateway_v1_task_tools_proto_init() }
|
||||
func file_gateway_v1_task_tools_proto_init() {
|
||||
if File_gateway_v1_task_tools_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_gateway_v1_task_tools_proto_rawDesc), len(file_gateway_v1_task_tools_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 6,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_gateway_v1_task_tools_proto_goTypes,
|
||||
DependencyIndexes: file_gateway_v1_task_tools_proto_depIdxs,
|
||||
MessageInfos: file_gateway_v1_task_tools_proto_msgTypes,
|
||||
}.Build()
|
||||
File_gateway_v1_task_tools_proto = out.File
|
||||
file_gateway_v1_task_tools_proto_goTypes = nil
|
||||
file_gateway_v1_task_tools_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.2
|
||||
// - protoc (unknown)
|
||||
// source: gateway/v1/task_tools.proto
|
||||
|
||||
package gatewayv1
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
TaskToolsService_ListTaskTools_FullMethodName = "/gateway.v1.TaskToolsService/ListTaskTools"
|
||||
TaskToolsService_CallTaskTool_FullMethodName = "/gateway.v1.TaskToolsService/CallTaskTool"
|
||||
TaskToolsService_CloseTaskTools_FullMethodName = "/gateway.v1.TaskToolsService/CloseTaskTools"
|
||||
)
|
||||
|
||||
// TaskToolsServiceClient is the client API for TaskToolsService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// TaskToolsService executes central tools for a live task grant. Only the
|
||||
// daemon calls it; identities, orgs and permissions are resolved by the server.
|
||||
type TaskToolsServiceClient interface {
|
||||
ListTaskTools(ctx context.Context, in *ListTaskToolsRequest, opts ...grpc.CallOption) (*ListTaskToolsResponse, error)
|
||||
CallTaskTool(ctx context.Context, in *CallTaskToolRequest, opts ...grpc.CallOption) (*CallTaskToolResponse, error)
|
||||
CloseTaskTools(ctx context.Context, in *CloseTaskToolsRequest, opts ...grpc.CallOption) (*CloseTaskToolsResponse, error)
|
||||
}
|
||||
|
||||
type taskToolsServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewTaskToolsServiceClient(cc grpc.ClientConnInterface) TaskToolsServiceClient {
|
||||
return &taskToolsServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *taskToolsServiceClient) ListTaskTools(ctx context.Context, in *ListTaskToolsRequest, opts ...grpc.CallOption) (*ListTaskToolsResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(ListTaskToolsResponse)
|
||||
err := c.cc.Invoke(ctx, TaskToolsService_ListTaskTools_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *taskToolsServiceClient) CallTaskTool(ctx context.Context, in *CallTaskToolRequest, opts ...grpc.CallOption) (*CallTaskToolResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CallTaskToolResponse)
|
||||
err := c.cc.Invoke(ctx, TaskToolsService_CallTaskTool_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *taskToolsServiceClient) CloseTaskTools(ctx context.Context, in *CloseTaskToolsRequest, opts ...grpc.CallOption) (*CloseTaskToolsResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CloseTaskToolsResponse)
|
||||
err := c.cc.Invoke(ctx, TaskToolsService_CloseTaskTools_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// TaskToolsServiceServer is the server API for TaskToolsService service.
|
||||
// All implementations must embed UnimplementedTaskToolsServiceServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// TaskToolsService executes central tools for a live task grant. Only the
|
||||
// daemon calls it; identities, orgs and permissions are resolved by the server.
|
||||
type TaskToolsServiceServer interface {
|
||||
ListTaskTools(context.Context, *ListTaskToolsRequest) (*ListTaskToolsResponse, error)
|
||||
CallTaskTool(context.Context, *CallTaskToolRequest) (*CallTaskToolResponse, error)
|
||||
CloseTaskTools(context.Context, *CloseTaskToolsRequest) (*CloseTaskToolsResponse, error)
|
||||
mustEmbedUnimplementedTaskToolsServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedTaskToolsServiceServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedTaskToolsServiceServer struct{}
|
||||
|
||||
func (UnimplementedTaskToolsServiceServer) ListTaskTools(context.Context, *ListTaskToolsRequest) (*ListTaskToolsResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ListTaskTools not implemented")
|
||||
}
|
||||
func (UnimplementedTaskToolsServiceServer) CallTaskTool(context.Context, *CallTaskToolRequest) (*CallTaskToolResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CallTaskTool not implemented")
|
||||
}
|
||||
func (UnimplementedTaskToolsServiceServer) CloseTaskTools(context.Context, *CloseTaskToolsRequest) (*CloseTaskToolsResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CloseTaskTools not implemented")
|
||||
}
|
||||
func (UnimplementedTaskToolsServiceServer) mustEmbedUnimplementedTaskToolsServiceServer() {}
|
||||
func (UnimplementedTaskToolsServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeTaskToolsServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to TaskToolsServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeTaskToolsServiceServer interface {
|
||||
mustEmbedUnimplementedTaskToolsServiceServer()
|
||||
}
|
||||
|
||||
func RegisterTaskToolsServiceServer(s grpc.ServiceRegistrar, srv TaskToolsServiceServer) {
|
||||
// If the following call panics, it indicates UnimplementedTaskToolsServiceServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&TaskToolsService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _TaskToolsService_ListTaskTools_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ListTaskToolsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TaskToolsServiceServer).ListTaskTools(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: TaskToolsService_ListTaskTools_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TaskToolsServiceServer).ListTaskTools(ctx, req.(*ListTaskToolsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _TaskToolsService_CallTaskTool_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CallTaskToolRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TaskToolsServiceServer).CallTaskTool(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: TaskToolsService_CallTaskTool_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TaskToolsServiceServer).CallTaskTool(ctx, req.(*CallTaskToolRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _TaskToolsService_CloseTaskTools_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CloseTaskToolsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TaskToolsServiceServer).CloseTaskTools(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: TaskToolsService_CloseTaskTools_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TaskToolsServiceServer).CloseTaskTools(ctx, req.(*CloseTaskToolsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// TaskToolsService_ServiceDesc is the grpc.ServiceDesc for TaskToolsService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var TaskToolsService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "gateway.v1.TaskToolsService",
|
||||
HandlerType: (*TaskToolsServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "ListTaskTools",
|
||||
Handler: _TaskToolsService_ListTaskTools_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CallTaskTool",
|
||||
Handler: _TaskToolsService_CallTaskTool_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CloseTaskTools",
|
||||
Handler: _TaskToolsService_CloseTaskTools_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "gateway/v1/task_tools.proto",
|
||||
}
|
||||
@@ -250,8 +250,10 @@ type ResolveTaskGrantResponse struct {
|
||||
TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
|
||||
// Canonical execution identity; empty names the instance root RPC route.
|
||||
ExecutionIdentity string `protobuf:"bytes,2,opt,name=execution_identity,json=executionIdentity,proto3" json:"execution_identity,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
// Canonical principal, including the instance root.
|
||||
PrincipalId string `protobuf:"bytes,3,opt,name=principal_id,json=principalId,proto3" json:"principal_id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ResolveTaskGrantResponse) Reset() {
|
||||
@@ -298,6 +300,13 @@ func (x *ResolveTaskGrantResponse) GetExecutionIdentity() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ResolveTaskGrantResponse) GetPrincipalId() string {
|
||||
if x != nil {
|
||||
return x.PrincipalId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Identity struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
@@ -19429,10 +19438,11 @@ const file_state_v1_state_proto_rawDesc = "" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\"\x19\n" +
|
||||
"\x17RevokeTaskGrantResponse\"/\n" +
|
||||
"\x17ResolveTaskGrantRequest\x12\x14\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\"b\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\"\x85\x01\n" +
|
||||
"\x18ResolveTaskGrantResponse\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12-\n" +
|
||||
"\x12execution_identity\x18\x02 \x01(\tR\x11executionIdentity\"\xff\x01\n" +
|
||||
"\x12execution_identity\x18\x02 \x01(\tR\x11executionIdentity\x12!\n" +
|
||||
"\fprincipal_id\x18\x03 \x01(\tR\vprincipalId\"\xff\x01\n" +
|
||||
"\bIdentity\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
|
||||
"\x04kind\x18\x02 \x01(\tR\x04kind\x12!\n" +
|
||||
|
||||
@@ -1937,12 +1937,12 @@ func (d *Daemon) pinTaskProfile(ctx context.Context, task *workflow.Task) (confi
|
||||
// validateProfileMeetsNeeds rejects a Kit profile whose harness cannot serve
|
||||
// the captures the workflow needs. Image profiles always pass.
|
||||
func validateProfileMeetsNeeds(profile config.AgentProfile, needs workflowtask.WorkflowNeeds) error {
|
||||
if !profile.IsKit() || !needs.Captures {
|
||||
if !profile.IsKit() || (!needs.Captures && len(profile.Tools) == 0) {
|
||||
return nil
|
||||
}
|
||||
adapter, ok := agentexec.LookupHarnessAdapter(profile.Adapter)
|
||||
if !ok || len(adapter.MCPConfig) == 0 {
|
||||
return fmt.Errorf("needs.captures is set, but profile adapter %q serves no capture tools", profile.Adapter)
|
||||
return fmt.Errorf("profile adapter %q cannot register the required Archie MCP tools", profile.Adapter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ func (d *Daemon) removeEndedKitVolumes(ctx context.Context, taskID int64, volume
|
||||
// through the egress proxy, which swaps in the org's bound key.
|
||||
type ModelEgress interface {
|
||||
Open(token, org string, providers map[string]config.Provider) (ModelSession, error)
|
||||
Close(token string)
|
||||
Close(ctx context.Context, token string)
|
||||
}
|
||||
|
||||
// TaskModels answers what a task's org resolves models against.
|
||||
@@ -174,5 +174,5 @@ func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, crede
|
||||
if err != nil {
|
||||
return ModelSession{}, nil, err
|
||||
}
|
||||
return session, func() { d.ModelEgress.Close(credential) }, nil
|
||||
return session, func() { d.ModelEgress.Close(ctx, credential) }, nil
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -21,10 +20,7 @@ type PackageAuthorities interface {
|
||||
func (d *Daemon) toolAllowlist(ctx context.Context, task *workflow.Task, profileTools []string) []string {
|
||||
name := workflow.PackageOf(task.WorkflowDefinitionYAML)
|
||||
if name == "" {
|
||||
if len(profileTools) == 0 {
|
||||
return nil
|
||||
}
|
||||
return profileTools
|
||||
return storepkg.AllowedTools(profileTools, false, nil)
|
||||
}
|
||||
var accepted []string
|
||||
if d.PackageAuthorities != nil {
|
||||
@@ -35,11 +31,5 @@ func (d *Daemon) toolAllowlist(ctx context.Context, task *workflow.Task, profile
|
||||
accepted = authority.Tools
|
||||
}
|
||||
}
|
||||
allowed := []string{}
|
||||
for _, tool := range accepted {
|
||||
if len(profileTools) == 0 || slices.Contains(profileTools, tool) {
|
||||
allowed = append(allowed, tool)
|
||||
}
|
||||
}
|
||||
return allowed
|
||||
return storepkg.AllowedTools(profileTools, true, accepted)
|
||||
}
|
||||
@@ -106,6 +106,8 @@ type HarnessSpec struct {
|
||||
// carrying {{.MCPConfig}}, the path of a standard mcpServers config.
|
||||
// A stage with capture tools needs it.
|
||||
MCPConfig []string `json:"mcp_config,omitempty"`
|
||||
// CentralTools enables the proxy-only central MCP endpoint.
|
||||
CentralTools bool `json:"central_tools,omitempty"`
|
||||
}
|
||||
|
||||
// Validate rejects requests that this runner cannot safely interpret.
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package storepkg
|
||||
|
||||
import "slices"
|
||||
|
||||
// AllowedTools intersects a profile's tools with accepted package authority.
|
||||
// Nil means unrestricted; a non-nil empty slice means no tools.
|
||||
func AllowedTools(profile []string, packaged bool, accepted []string) []string {
|
||||
if !packaged {
|
||||
if len(profile) == 0 {
|
||||
return nil
|
||||
}
|
||||
return slices.Clone(profile)
|
||||
}
|
||||
allowed := []string{}
|
||||
for _, name := range accepted {
|
||||
if len(profile) == 0 || slices.Contains(profile, name) {
|
||||
allowed = append(allowed, name)
|
||||
}
|
||||
}
|
||||
return allowed
|
||||
}
|
||||
@@ -41,13 +41,16 @@ func TestCredentialPhaseEnforcement(t *testing.T) {
|
||||
{Service: "key", Phase: phases, APIKey: &spec.APIKey{Inject: []spec.Inject{{Domain: "api.example", Header: "X-Key"}}}},
|
||||
{Service: "oauth", Phase: phases, OAuth: &spec.OAuth{TokenEndpoint: &spec.TokenEndpoint{Host: "token.example", Path: "/token"}, ResourceHosts: []string{"api.example"}, Sentinels: &spec.Sentinels{AccessToken: "sentinel"}}},
|
||||
}
|
||||
proxy := NewProxy(nil, ProxyOptions{Resolver: ResolverFunc(func(context.Context, string, string) (string, error) {
|
||||
proxy := NewProxy(nil, ProxyOptions{Resolver: ResolverFunc(func(_ context.Context, credential, _ string) (string, error) {
|
||||
if credential != "host-run-grant" {
|
||||
t.Fatal("resolver received the container proxy password instead of the host grant")
|
||||
}
|
||||
if tc.revoked {
|
||||
return "", ErrUnbound
|
||||
}
|
||||
return "real-key", nil
|
||||
}), OAuthStore: phaseOAuthStore{}})
|
||||
session := &Session{token: "run", org: "org", injections: compileInjections(creds, nil), oauth: compileOAuthRules(creds, nil)}
|
||||
session := &Session{token: "proxy-password", runCredential: "host-run-grant", org: "org", injections: compileInjections(creds, nil), oauth: compileOAuthRules(creds, nil)}
|
||||
for _, runtime := range []bool{false, true} {
|
||||
session.atRun.Store(runtime)
|
||||
want := tc.install
|
||||
|
||||
@@ -97,7 +97,7 @@ func (p *Proxy) inject(ctx context.Context, s *Session, r *http.Request, host st
|
||||
}
|
||||
continue
|
||||
}
|
||||
secret, err := p.resolver.Resolve(ctx, s.token, rule.service)
|
||||
secret, err := p.resolver.Resolve(ctx, s.credential(), rule.service)
|
||||
switch {
|
||||
case errors.Is(err, ErrUnbound) && !rule.required:
|
||||
continue
|
||||
|
||||
@@ -110,7 +110,7 @@ func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool,
|
||||
case p.resolver == nil:
|
||||
err = ErrUnbound
|
||||
default:
|
||||
_, err = p.resolver.Resolve(ctx, s.token, rule.service)
|
||||
_, err = p.resolver.Resolve(ctx, s.credential(), rule.service)
|
||||
}
|
||||
switch {
|
||||
case err == nil:
|
||||
|
||||
@@ -2,9 +2,11 @@ package egress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -20,6 +22,7 @@ import (
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
||||
)
|
||||
|
||||
// proxyUser is the fixed user name in a sandbox's proxy URL; the session
|
||||
@@ -38,6 +41,9 @@ var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)}
|
||||
type Session struct {
|
||||
token string
|
||||
runCredential string
|
||||
gatewayToken string
|
||||
lifetime context.Context //nolint:containedctx // Session lifetime cancels active HTTP requests.
|
||||
cancel context.CancelFunc
|
||||
principal access.Principal
|
||||
org string
|
||||
install rules
|
||||
@@ -48,8 +54,7 @@ type Session struct {
|
||||
atRun atomic.Bool
|
||||
}
|
||||
|
||||
// Token is the secret the container presents as its proxy password: its run
|
||||
// credential.
|
||||
// Token is the proxy password the container may hold; it is not its task grant.
|
||||
func (s *Session) Token() string { return s.token }
|
||||
|
||||
// EnterRuntime switches the session from install-phase to runtime-phase
|
||||
@@ -82,14 +87,14 @@ type ProxyOptions struct {
|
||||
// OAuthStore holds the org token sets OAuth credentials use. Without
|
||||
// one, Register refuses a session with a required OAuth credential.
|
||||
OAuthStore OAuthStore
|
||||
TaskTools tasktools.Backend
|
||||
}
|
||||
|
||||
// SessionOptions describe one container's run: the run credential it acts
|
||||
// under, the org whose OAuth token sets it uses, and its Kit's network
|
||||
// policy and credential requests.
|
||||
type SessionOptions struct {
|
||||
// Token is the run credential: the container's proxy password, and what
|
||||
// the Resolver resolves the run from.
|
||||
// Token is the container's proxy password. RunCredential stays on the host.
|
||||
Token string
|
||||
// RunCredential scopes task OAuth operations; setup sessions forward Principal.
|
||||
RunCredential string
|
||||
@@ -116,6 +121,7 @@ type Proxy struct {
|
||||
transport *http.Transport
|
||||
resolver Resolver
|
||||
oauth OAuthStore
|
||||
taskTools tasktools.Backend
|
||||
|
||||
mu sync.RWMutex
|
||||
sessions map[string]*Session
|
||||
@@ -124,7 +130,7 @@ type Proxy struct {
|
||||
type sessionKey struct{}
|
||||
|
||||
func NewProxy(ca *CA, opts ProxyOptions) *Proxy {
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, oauth: opts.OAuthStore, sessions: map[string]*Session{}}
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, oauth: opts.OAuthStore, taskTools: opts.TaskTools, sessions: map[string]*Session{}}
|
||||
p.transport = &http.Transport{
|
||||
DialContext: p.dialUpstream,
|
||||
TLSClientConfig: &tls.Config{RootCAs: opts.UpstreamRoots, MinVersion: tls.VersionTLS12},
|
||||
@@ -145,6 +151,14 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
|
||||
injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound),
|
||||
substitutions: compileSubstitutions(opts.Substitutions),
|
||||
}
|
||||
if opts.RunCredential != "" && p.taskTools != nil {
|
||||
var token [32]byte
|
||||
if _, err := rand.Read(token[:]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.gatewayToken = hex.EncodeToString(token[:])
|
||||
s.lifetime, s.cancel = context.WithCancel(context.Background())
|
||||
}
|
||||
for _, rule := range s.oauth {
|
||||
if rule.required && p.oauth == nil {
|
||||
return nil, fmt.Errorf("credential %q is OAuth-managed and no harness secret store is configured", rule.service)
|
||||
@@ -160,10 +174,17 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
|
||||
}
|
||||
|
||||
// Revoke ends a session. Requests already forwarded finish; new ones fail.
|
||||
func (p *Proxy) Revoke(token string) {
|
||||
func (p *Proxy) Revoke(parent context.Context, token string) {
|
||||
p.mu.Lock()
|
||||
s := p.sessions[token]
|
||||
delete(p.sessions, token)
|
||||
p.mu.Unlock()
|
||||
if s != nil && s.cancel != nil {
|
||||
s.cancel()
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second)
|
||||
defer cancel()
|
||||
_ = p.taskTools.Close(ctx, s.runCredential)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Proxy) session(r *http.Request) *Session {
|
||||
@@ -191,6 +212,10 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "proxy authentication required", http.StatusProxyAuthRequired)
|
||||
return
|
||||
}
|
||||
if normalizeHost(r.URL.Hostname()) == "archie.tools" {
|
||||
p.serveTaskTools(r.Context(), w, r, s)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case r.Method == http.MethodConnect:
|
||||
p.serveConnect(w, r, s)
|
||||
@@ -435,3 +460,32 @@ func (c *closeNotifyConn) Close() error {
|
||||
c.once.Do(func() { close(c.done) })
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (p *Proxy) serveTaskTools(parent context.Context, w http.ResponseWriter, r *http.Request, s *Session) {
|
||||
if s.gatewayToken == "" || s.phase() != "runtime" || r.URL.String() != tasktools.Endpoint || r.Host != "archie.tools" {
|
||||
http.Error(w, "task tools are unavailable for this session or target", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Authorization") != "Bearer "+tasktools.Sentinel {
|
||||
http.Error(w, "task tools sentinel required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
// Only this authenticated proxy hop supplies the host-held gateway token.
|
||||
r = r.Clone(parent)
|
||||
r.Header.Set("Authorization", "Bearer "+s.gatewayToken)
|
||||
r.Header.Del("Proxy-Authorization")
|
||||
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
defer cancel()
|
||||
stop := context.AfterFunc(s.lifetime, cancel) //nolint:contextcheck // Session revocation is an additional cancellation source.
|
||||
defer stop()
|
||||
tasktools.ServeHTTP(ctx, w, r, p.taskTools, s.runCredential, s.gatewayToken)
|
||||
}
|
||||
|
||||
// credential is the host-held task grant, or a setup session's own grant.
|
||||
func (s *Session) credential() string {
|
||||
if s.runCredential != "" {
|
||||
return s.runCredential
|
||||
}
|
||||
return s.token
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func (p *Proxy) substitute(ctx context.Context, s *Session, r *http.Request, hos
|
||||
if p.resolver == nil {
|
||||
return fmt.Errorf("credential %q has no resolver", sub.service)
|
||||
}
|
||||
secret, err := p.resolver.Resolve(ctx, s.token, sub.service)
|
||||
secret, err := p.resolver.Resolve(ctx, s.credential(), sub.service)
|
||||
if errors.Is(err, ErrUnbound) {
|
||||
return fmt.Errorf("credential %q is not bound for this run", sub.service)
|
||||
}
|
||||
|
||||
@@ -43,12 +43,18 @@ func TestSubstitutionReachesOnlyTheServiceHost(t *testing.T) {
|
||||
resolve = func() (string, error) { return "real-key", nil }
|
||||
}
|
||||
proxy := NewProxy(nil, ProxyOptions{
|
||||
Resolver: ResolverFunc(func(context.Context, string, string) (string, error) { return resolve() }),
|
||||
Resolver: ResolverFunc(func(_ context.Context, credential, _ string) (string, error) {
|
||||
if credential != "host-run-grant" {
|
||||
t.Error("resolver received the proxy password")
|
||||
return "", ErrUnbound
|
||||
}
|
||||
return resolve()
|
||||
}),
|
||||
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
||||
},
|
||||
})
|
||||
session := &Session{token: "run-token", substitutions: compileSubstitutions([]Substitution{{Service: "openai", Host: "api.openai.example"}})}
|
||||
session := &Session{token: "proxy-password", runCredential: "host-run-grant", substitutions: compileSubstitutions([]Substitution{{Service: "openai", Host: "api.openai.example"}})}
|
||||
|
||||
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "http://"+tc.host+tc.target, nil)
|
||||
req.Header.Set("Authorization", "Bearer "+SentinelFor("openai"))
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
||||
)
|
||||
|
||||
type proxyTaskTools struct{ calls int }
|
||||
|
||||
func (b *proxyTaskTools) List(context.Context, string) ([]*mcp.Tool, error) {
|
||||
b.calls++
|
||||
return []*mcp.Tool{}, nil
|
||||
}
|
||||
|
||||
func (*proxyTaskTools) Call(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) {
|
||||
return &mcp.CallToolResult{}, nil
|
||||
}
|
||||
func (*proxyTaskTools) Close(context.Context, string) error { return nil }
|
||||
|
||||
func TestTaskToolsProxyConfinement(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, target, auth string
|
||||
task, runtime, revoked bool
|
||||
want int
|
||||
}{
|
||||
{name: "runtime", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusOK},
|
||||
{name: "install", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, want: http.StatusForbidden},
|
||||
{name: "setup", target: tasktools.Endpoint, auth: tasktools.Sentinel, runtime: true, want: http.StatusForbidden},
|
||||
{name: "revoked", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, revoked: true, want: http.StatusProxyAuthRequired},
|
||||
{name: "forged bearer", target: tasktools.Endpoint, auth: "forged", task: true, runtime: true, want: http.StatusUnauthorized},
|
||||
{name: "other path", target: "http://archie.tools/other", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
||||
{name: "query", target: tasktools.Endpoint + "?target=victim", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
||||
{name: "other port", target: "http://archie.tools:8080/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
||||
{name: "other host", target: "http://attacker.example/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
backend := &proxyTaskTools{}
|
||||
proxy := NewProxy(nil, ProxyOptions{TaskTools: backend})
|
||||
opts := SessionOptions{Token: "proxy-password"}
|
||||
if tt.task {
|
||||
opts.RunCredential = "run-grant"
|
||||
}
|
||||
session, err := proxy.Register(opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.runtime {
|
||||
session.EnterRuntime()
|
||||
}
|
||||
if tt.revoked {
|
||||
proxy.Revoke(t.Context(), session.Token())
|
||||
}
|
||||
if session.gatewayToken != "" && (session.gatewayToken == session.Token() || session.gatewayToken == tasktools.Sentinel) {
|
||||
t.Fatal("gateway token is container-visible")
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, tt.target, strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}`))
|
||||
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("archie:"+session.Token())))
|
||||
req.Header.Set("Authorization", "Bearer "+tt.auth)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json, text/event-stream")
|
||||
response := httptest.NewRecorder()
|
||||
proxy.ServeHTTP(response, req)
|
||||
if response.Code != tt.want {
|
||||
t.Fatalf("status %d, want %d: %s", response.Code, tt.want, response.Body.String())
|
||||
}
|
||||
if tt.want != http.StatusOK && backend.calls != 0 {
|
||||
t.Fatal("rejected request reached gateway")
|
||||
}
|
||||
if session.gatewayToken != "" && strings.Contains(response.Body.String(), session.gatewayToken) {
|
||||
t.Fatal("gateway token escaped in response")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -108,7 +108,8 @@ type Request struct {
|
||||
// other.
|
||||
Org string
|
||||
GrantedServices []string
|
||||
// RunCredential is the run's credential: the container's proxy password.
|
||||
// RunCredential is the worker grant, retained only on the host and in the
|
||||
// worker environment. The harness gets a separate proxy password.
|
||||
RunCredential string
|
||||
}
|
||||
|
||||
@@ -180,13 +181,13 @@ func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, err
|
||||
// harness environment.
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: req.Session, ProxyToken: session.Token(), CAPath: caPath})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, err
|
||||
}
|
||||
run.Harness, run.Volumes = launch.Harness, launch.Volumes
|
||||
l.Grants.Grant(session.Token(), req.Org, services)
|
||||
if err := l.Networks.Create(ctx, run.network); err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, err
|
||||
}
|
||||
// The container runs archie's own agent as a keepalive rather than the
|
||||
@@ -207,7 +208,7 @@ func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, err
|
||||
InstallDone: session.EnterRuntime,
|
||||
})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, errors.Join(err, l.Networks.Remove(context.WithoutCancel(ctx), run.network))
|
||||
}
|
||||
return run, nil
|
||||
@@ -235,8 +236,7 @@ func orgCredentialKinds(bindings []config.CredentialBinding, org string, creds [
|
||||
return kinds, services
|
||||
}
|
||||
|
||||
// newProxyToken mints a setup session's run credential. It is unguessable
|
||||
// because it is the container's proxy password.
|
||||
// newProxyToken mints a container proxy password, separate from worker grants.
|
||||
func newProxyToken() (string, error) {
|
||||
var b [32]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
@@ -278,20 +278,21 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session, err := l.Proxy.Register(egress.SessionOptions{RunCredential: req.RunCredential, Token: req.RunCredential, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
|
||||
session, err := l.taskSession(req, k, kinds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
run := &Run{network: containerName, token: session.Token(), execution: execution}
|
||||
launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Execution: execution, ProxyToken: session.Token(), CAPath: caPath, Bound: kinds, OAuth: facts})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, err
|
||||
}
|
||||
launch.Harness.Adapter, launch.Harness.MCPConfig = req.Adapter, adapter.MCPConfig
|
||||
launch.Harness.CentralTools = len(adapter.MCPConfig) > 0
|
||||
run.Harness, run.Volumes = launch.Harness, launch.Volumes
|
||||
if err := l.Networks.Create(ctx, run.network); err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, err
|
||||
}
|
||||
run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{
|
||||
@@ -309,7 +310,7 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
InstallDone: session.EnterRuntime,
|
||||
})
|
||||
if err != nil {
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
return nil, errors.Join(err, l.Networks.Remove(context.WithoutCancel(ctx), run.network))
|
||||
}
|
||||
return run, nil
|
||||
@@ -374,8 +375,8 @@ func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.Cred
|
||||
// release undoes what Launch already did for run: its proxy session. Call sites past this point pass the network/container errors
|
||||
// through their own cleanup; release only ever needs to run once per Launch
|
||||
// failure, so it takes no error to join.
|
||||
func (l *Launcher) release(run *Run) {
|
||||
l.Proxy.Revoke(run.token)
|
||||
func (l *Launcher) release(ctx context.Context, run *Run) {
|
||||
l.Proxy.Revoke(ctx, run.token)
|
||||
if run.grants != nil {
|
||||
run.grants.Revoke(run.token)
|
||||
}
|
||||
@@ -443,7 +444,7 @@ func (l *Launcher) skillsDir() string {
|
||||
// them. A task run keeps its volumes for the life of its execution.
|
||||
func (l *Launcher) Release(ctx context.Context, run *Run) error {
|
||||
l.Pool.Release(ctx, run.Container)
|
||||
l.release(run)
|
||||
l.release(ctx, run)
|
||||
err := l.Networks.Remove(context.WithoutCancel(ctx), run.network)
|
||||
if run.ephemeral {
|
||||
err = errors.Join(err, l.RemoveVolumes(context.WithoutCancel(ctx), run.Volumes))
|
||||
@@ -510,3 +511,14 @@ func skillsBinds(asks []spec.AgentSkillsCapability, sharedDir string) []string {
|
||||
}
|
||||
return binds
|
||||
}
|
||||
|
||||
func (l *Launcher) taskSession(req Request, k kitNeeds, kinds map[string]egress.CredentialKind) (*egress.Session, error) {
|
||||
if req.RunCredential == "" {
|
||||
return nil, errors.New("kit tasks require a run credential")
|
||||
}
|
||||
token, err := newProxyToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l.Proxy.Register(egress.SessionOptions{RunCredential: req.RunCredential, Token: token, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
|
||||
}
|
||||
@@ -3,12 +3,14 @@ package kitrun
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/kit"
|
||||
"github.com/samcharles93/archie-core/internal/skill"
|
||||
)
|
||||
|
||||
@@ -103,3 +105,27 @@ func TestSharedSkillsDoNotMaskImageDiscovery(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarnessProxyPasswordDoesNotExposeRunGrant(t *testing.T) {
|
||||
l := &Launcher{Proxy: egress.NewProxy(nil, egress.ProxyOptions{})}
|
||||
req := Request{RunCredential: "worker-state-store-grant", Org: "acme"}
|
||||
session, err := l.taskSession(req, kitNeeds{}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer l.Proxy.Revoke(t.Context(), session.Token())
|
||||
if session.Token() == req.RunCredential {
|
||||
t.Fatal("harness proxy password exposes the worker's State Store grant")
|
||||
}
|
||||
launch, err := kit.Assemble(&kit.Plan{}, kit.ImageConfig{}, kit.LaunchParams{ProxyToken: session.Token()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := strings.Join(launch.Harness.Env, "\n")
|
||||
if strings.Contains(env, req.RunCredential) {
|
||||
t.Fatal("worker grant entered the harness environment")
|
||||
}
|
||||
if !strings.Contains(env, session.Token()) {
|
||||
t.Fatal("harness has no proxy password")
|
||||
}
|
||||
}
|
||||
@@ -268,7 +268,7 @@ func (s *server) ResolveTaskGrant(ctx context.Context, r *pb.ResolveTaskGrantReq
|
||||
if principal == s.deps.RootIdentity {
|
||||
route = ""
|
||||
}
|
||||
return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: route}, nil
|
||||
return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: route, PrincipalId: string(principal)}, nil
|
||||
}
|
||||
|
||||
// RegisterTaskGrant creates fresh random material at the daemon and registers
|
||||
@@ -409,3 +409,13 @@ func (s *server) executionPrincipal(ctx context.Context, t *task.Task) (identity
|
||||
}
|
||||
return value.ID, nil
|
||||
}
|
||||
|
||||
// TaskPrincipalForCredential resolves the task and its canonical execution principal.
|
||||
func (c *Client) TaskPrincipalForCredential(ctx context.Context, token string) (*task.Task, identity.IdentityID, error) {
|
||||
r, err := c.client.ResolveTaskGrant(ctx, &pb.ResolveTaskGrantRequest{Token: token})
|
||||
if err != nil {
|
||||
return nil, "", unmapError(err)
|
||||
}
|
||||
t, err := c.TaskByID(ctx, r.TaskId)
|
||||
return t, identity.IdentityID(r.PrincipalId), err
|
||||
}
|
||||
+26
-18
@@ -1,4 +1,4 @@
|
||||
package agentworker
|
||||
package taskmcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
@@ -17,38 +16,41 @@ import (
|
||||
|
||||
var errUnknownMCPTransport = errors.New("unknown MCP transport")
|
||||
|
||||
// mcpProviderSet holds the resources needed to clean up MCP providers.
|
||||
type mcpProviderSet struct {
|
||||
// Set holds the resources needed to clean up MCP providers.
|
||||
type Set struct {
|
||||
providers []toolprovider.Engine
|
||||
registry *tools.Registry
|
||||
Registry *tools.Registry
|
||||
}
|
||||
|
||||
type mcpProviderBuilder func(config.MCPServer, mcp.SamplingHandler) (toolprovider.Engine, string, error)
|
||||
|
||||
// startMCPProviders starts the configured MCP servers and registers their
|
||||
// Start starts the configured MCP servers and registers their
|
||||
// tools. sampling answers sampling requests when non-nil. Returns nil with
|
||||
// no servers.
|
||||
func startMCPProviders(
|
||||
func Start(
|
||||
ctx context.Context,
|
||||
servers []config.MCPServer,
|
||||
sampling mcp.SamplingHandler,
|
||||
log *slog.Logger,
|
||||
) (*mcpProviderSet, error) {
|
||||
return startMCPProvidersWith(ctx, servers, sampling, log, buildMCPProvider)
|
||||
cacheDir string,
|
||||
) (*Set, error) {
|
||||
return startWith(ctx, servers, sampling, log, func(server config.MCPServer, sampling mcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
return NewProvider(server, sampling, cacheDir)
|
||||
})
|
||||
}
|
||||
|
||||
func startMCPProvidersWith(
|
||||
func startWith(
|
||||
ctx context.Context,
|
||||
servers []config.MCPServer,
|
||||
sampling mcp.SamplingHandler,
|
||||
log *slog.Logger,
|
||||
build mcpProviderBuilder,
|
||||
) (*mcpProviderSet, error) {
|
||||
) (*Set, error) {
|
||||
if len(servers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
set := &mcpProviderSet{registry: tools.NewRegistry()}
|
||||
set := &Set{Registry: tools.NewRegistry()}
|
||||
var firstErr error
|
||||
for _, server := range servers {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
@@ -68,7 +70,7 @@ func startMCPProvidersWith(
|
||||
continue
|
||||
}
|
||||
|
||||
toolCount, err := startMCPProvider(ctx, name, provider, set.registry, log)
|
||||
toolCount, err := startMCPProvider(ctx, name, provider, set.Registry, log)
|
||||
if err != nil {
|
||||
firstErr = retainFirstError(firstErr, err)
|
||||
continue
|
||||
@@ -84,14 +86,18 @@ func startMCPProvidersWith(
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func buildMCPProvider(server config.MCPServer, sampling mcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
// NewProvider builds the same transport and tool names for chat and tasks.
|
||||
func NewProvider(server config.MCPServer, sampling mcp.SamplingHandler, cacheDir string) (toolprovider.Engine, string, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
transportType := strings.ToLower(strings.TrimSpace(server.Transport))
|
||||
if transportType == "" {
|
||||
transportType = "stdio"
|
||||
}
|
||||
|
||||
transport, err := mcpTransportForServer(name, transportType, server)
|
||||
if name == "" {
|
||||
return nil, transportType, fmt.Errorf("MCP server name is required")
|
||||
}
|
||||
transport, err := mcpTransportForServer(name, transportType, server, cacheDir)
|
||||
if err != nil {
|
||||
return nil, transportType, err
|
||||
}
|
||||
@@ -102,6 +108,7 @@ func mcpTransportForServer(
|
||||
name string,
|
||||
transportType string,
|
||||
server config.MCPServer,
|
||||
cacheDir string,
|
||||
) (mcptoolprovider.LifecycleTransport, error) {
|
||||
switch transportType {
|
||||
case "stdio":
|
||||
@@ -109,11 +116,12 @@ func mcpTransportForServer(
|
||||
if command == "" {
|
||||
return nil, fmt.Errorf("MCP stdio server %q requires a command", name)
|
||||
}
|
||||
// Share the npm cache volume across task containers.
|
||||
// Keep npx packages in the caller's cache.
|
||||
return mcp.NewStdioTransport(mcp.StdioTransportConfig{
|
||||
Command: command,
|
||||
Args: append([]string(nil), server.Args...),
|
||||
Env: mcp.NpmCacheEnv(command, storage.MCPNPMCacheMountDir),
|
||||
Dir: server.WorkDir,
|
||||
Env: mcp.NpmCacheEnv(command, cacheDir),
|
||||
}), nil
|
||||
case "http", "streamablehttp":
|
||||
url := strings.TrimSpace(server.URL)
|
||||
@@ -173,7 +181,7 @@ func retainFirstError(firstErr, err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *mcpProviderSet) cleanup(ctx context.Context, log *slog.Logger) {
|
||||
func (s *Set) Close(ctx context.Context, log *slog.Logger) {
|
||||
for _, provider := range s.providers {
|
||||
if err := provider.Stop(ctx); err != nil {
|
||||
log.Warn("mcp provider stop failed", "err", err)
|
||||
+5
-5
@@ -1,4 +1,4 @@
|
||||
package agentworker
|
||||
package taskmcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -18,10 +18,10 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// taskSamplingHandler answers MCP sampling requests with the task's model.
|
||||
// SamplingHandler answers MCP sampling requests with the task's model.
|
||||
// Without a runtime or model the request is refused. record receives each
|
||||
// call's model ref and token usage.
|
||||
func taskSamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig, record func(context.Context, string, chat.Usage)) mcp.SamplingHandler {
|
||||
func SamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig, record func(context.Context, string, chat.Usage)) mcp.SamplingHandler {
|
||||
return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) {
|
||||
if llm == nil {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no model runtime is configured")
|
||||
@@ -58,9 +58,9 @@ func taskSamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig, record fun
|
||||
}
|
||||
}
|
||||
|
||||
// samplingUsageRecorder records a task's MCP sampling calls against the task.
|
||||
// UsageRecorder records a task's MCP sampling calls against the task.
|
||||
// A failed write is logged, never fails the call.
|
||||
func samplingUsageRecorder(store workflow.Store, t *workflow.Task, log *slog.Logger) func(context.Context, string, chat.Usage) {
|
||||
func UsageRecorder(store usage.Recorder, t *workflow.Task, log *slog.Logger) func(context.Context, string, chat.Usage) {
|
||||
return func(ctx context.Context, model string, used chat.Usage) {
|
||||
if store == nil || t == nil {
|
||||
return
|
||||
@@ -0,0 +1,59 @@
|
||||
package taskmcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
func TestSamplingKeepsTaskCredentialsAndUsage(t *testing.T) {
|
||||
t.Setenv("ARCHIE_SAMPLING_FIXTURE_KEY", "instance-key")
|
||||
for _, key := range []string{"org-a-key", "org-b-key"} {
|
||||
t.Run(key, func(t *testing.T) {
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "Bearer "+key {
|
||||
t.Error("sampling used another org's key")
|
||||
}
|
||||
var input map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if input["model"] != "task-model" {
|
||||
t.Errorf("sampling model %v", input["model"])
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"sample","object":"chat.completion","model":"task-model","choices":[{"index":0,"message":{"role":"assistant","content":"sampled"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
providers := map[string]agentrun.Provider{"fixture": {Class: "openai", BaseURL: upstream.URL + "/v1", APIKeyEnv: "ARCHIE_SAMPLING_FIXTURE_KEY"}}
|
||||
llm := modelloop.NewRuntimeWithKeys(providers, nil, map[string]string{"fixture": key})
|
||||
recorded := false
|
||||
sample := SamplingHandler(llm, config.TaskConfig{Models: map[string]string{"default": "fixture/task-model"}}, func(_ context.Context, model string, usage chat.Usage) {
|
||||
recorded = true
|
||||
if model != "fixture/task-model" || usage.TotalTokens != 5 {
|
||||
t.Errorf("usage attribution %s %+v", model, usage)
|
||||
}
|
||||
})
|
||||
result, err := sample(t.Context(), protocol.SamplingRequest{Messages: []protocol.SamplingMessage{{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "sample"}}}, MaxTokens: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if os.Getenv("ARCHIE_SAMPLING_FIXTURE_KEY") != "instance-key" {
|
||||
t.Fatal("sampling changed process-wide credentials")
|
||||
}
|
||||
if !recorded || result.Model != "fixture/task-model" || result.Content.Text != "sampled" {
|
||||
t.Fatalf("sampling result %+v, recorded %v", result, recorded)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
syntax = "proto3";
|
||||
package gateway.v1;
|
||||
|
||||
// TaskToolsService executes central tools for a live task grant. Only the
|
||||
// daemon calls it; identities, orgs and permissions are resolved by the server.
|
||||
service TaskToolsService {
|
||||
rpc ListTaskTools(ListTaskToolsRequest) returns (ListTaskToolsResponse);
|
||||
rpc CallTaskTool(CallTaskToolRequest) returns (CallTaskToolResponse);
|
||||
rpc CloseTaskTools(CloseTaskToolsRequest) returns (CloseTaskToolsResponse);
|
||||
}
|
||||
message ListTaskToolsRequest { string run_credential = 1; }
|
||||
message ListTaskToolsResponse { bytes tools_json = 1; }
|
||||
message CallTaskToolRequest {
|
||||
string run_credential = 1;
|
||||
string name = 2;
|
||||
bytes arguments_json = 3;
|
||||
}
|
||||
message CallTaskToolResponse { bytes result_json = 1; }
|
||||
message CloseTaskToolsRequest { string run_credential = 1; }
|
||||
message CloseTaskToolsResponse {}
|
||||
@@ -328,6 +328,8 @@ message ResolveTaskGrantResponse {
|
||||
int64 task_id = 1;
|
||||
// Canonical execution identity; empty names the instance root RPC route.
|
||||
string execution_identity = 2;
|
||||
// Canonical principal, including the instance root.
|
||||
string principal_id = 3;
|
||||
}
|
||||
|
||||
message Identity {
|
||||
|
||||
Reference in new issue
Block a user