mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-26 01:41:04 +08:00
An agent process inherited the server's environment minus the names that ScrubSecretEnv removes: a deny list, so a credential under any other name (one an operator or an image adds) reached the agent's shell, its custom tools and its MCP stdio servers. When a process crosses from the server's identity to the executor's, its environment is now rebuilt from a fixed allowlist: the search path and locale, the temp directory, the non-interactive defaults, the proxy and CA trust variables the agents pass to their shells, PLAYWRIGHT_BROWSERS_PATH and the shim's own variables. The Bash shim and the MCP drop shim rebuild it before the drop; a custom tool starts from it. What the caller passes on purpose still crosses: the agent's Bash env, the TOOL_* parameters, and the configured env of an MCP server, which the drop shim keeps because connectMCP names it in ARTEX_EXEC_KEEP. A nested shell is not filtered again. The startup scrub and the environment seal stay as they are; they are no longer the boundary for agent processes. With the drop disabled nothing changes.
101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/mcphttp"
|
|
"github.com/Autumn-27/norma/mcp"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// mcpClient is the shared surface of a connected MCP server (stdio, Streamable HTTP,
|
|
// or legacy SSE),
|
|
// so tools/list and cleanup are handled uniformly regardless of transport.
|
|
type mcpClient interface {
|
|
Tools(context.Context) ([]actool.CoreTool, error)
|
|
Close() error
|
|
}
|
|
|
|
// connectMCP dials one MCP server per its transport. Callers must Close the client.
|
|
func connectMCP(ctx context.Context, m *db.MCPServer, priv ExecPrivilege) (mcpClient, error) {
|
|
switch m.Transport {
|
|
case "stdio":
|
|
if m.Command == "" {
|
|
return nil, fmt.Errorf("stdio transport is missing a command")
|
|
}
|
|
// The stdio command runs as the executor identity (see execpriv.go).
|
|
command, args, err := execDropCommand(priv, m.Command, jsonStrSlice(m.Args))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The drop shim rebuilds the environment from the allowlist; the configured
|
|
// variables of the server are named so it keeps them.
|
|
return mcp.NewStdioClient(ctx, m.Name, command, withExecKeep(priv, jsonStrMap(m.Env)), args...)
|
|
case "http":
|
|
if m.URL == "" {
|
|
return nil, fmt.Errorf("http transport is missing a URL")
|
|
}
|
|
// env map doubles as HTTP headers (e.g. Authorization).
|
|
return mcphttp.New(ctx, m.Name, m.URL, jsonStrMap(m.Env), m.Insecure, m.TimeoutSeconds)
|
|
case "sse":
|
|
if m.URL == "" {
|
|
return nil, fmt.Errorf("sse transport is missing a URL")
|
|
}
|
|
return mcphttp.NewSSE(ctx, m.Name, m.URL, jsonStrMap(m.Env), m.Insecure)
|
|
default:
|
|
return nil, fmt.Errorf("unknown transport %q", m.Transport)
|
|
}
|
|
}
|
|
|
|
// discoverAndCacheMCP connects to one MCP, lists its tools, and persists the tool
|
|
// names to mcp_tools_cache so the UI shows them without a live connection.
|
|
func (s *Server) discoverAndCacheMCP(ctx context.Context, m *db.MCPServer) error {
|
|
cl, err := connectMCP(ctx, m, s.execPriv)
|
|
if err != nil {
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, false, err.Error())
|
|
return err
|
|
}
|
|
defer cl.Close()
|
|
ts, err := cl.Tools(ctx)
|
|
if err != nil {
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, false, err.Error())
|
|
return err
|
|
}
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, true, "")
|
|
tools := make([]db.MCPTool, 0, len(ts))
|
|
for _, t := range ts {
|
|
tools = append(tools, db.MCPTool{Name: t.Name(), Description: t.Description()})
|
|
}
|
|
if err := s.m.pg.SaveMCPTools(m.ID, tools); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("[mcp] %s discovered %d tools and cached them", m.Name, len(tools))
|
|
return nil
|
|
}
|
|
|
|
// discoverEmptyMCPsOnStartup fills the tool cache for any enabled MCP that has none
|
|
// yet (notably the seeded browser MCP on first run). Runs sequentially in one
|
|
// goroutine so we never spawn many stdio servers at once, and never blocks
|
|
// startup. Best-effort: a failure leaves the cache empty to retry next start.
|
|
func (s *Server) discoverEmptyMCPsOnStartup() {
|
|
servers, err := s.m.pg.ListMCP()
|
|
if err != nil {
|
|
log.Printf("[mcp] startup auto-discovery: failed to read the list: %v", err)
|
|
return
|
|
}
|
|
for _, m := range servers {
|
|
if !m.Enabled || len(m.Tools) > 0 {
|
|
continue
|
|
}
|
|
ctx, cancel := context.WithTimeout(s.ctx, 90*time.Second)
|
|
if err := s.discoverAndCacheMCP(ctx, m); err != nil {
|
|
log.Printf("[mcp] startup auto-discovery of %s failed: %v", m.Name, err)
|
|
}
|
|
cancel()
|
|
}
|
|
}
|