// Package mcphttp is a remote (Streamable HTTP) MCP client for ARTEX. // // The core mcp package only speaks stdio; this adapts an HTTP/JSON-RPC MCP server // to the same tool.CoreTool shape (mcp__server__tool) WITHOUT touching the SDK. // It implements the MCP "Streamable HTTP" transport: JSON-RPC over HTTP POST, with // the server free to answer either as application/json or a text/event-stream (SSE) // carrying the response. Custom headers (e.g. Authorization) come from the MCP // row's env map, so a bearer token is configured as {"Authorization":"Bearer …"}. package mcphttp import ( "bufio" "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "sync" "time" "github.com/Autumn-27/norma/llm" "github.com/Autumn-27/norma/permission" actool "github.com/Autumn-27/norma/tool" ) const protocolVersion = "2025-06-18" // ToolName mirrors mcp.ToolName so remote tools share the mcp__server__tool scheme. func ToolName(server, name string) string { return "mcp__" + server + "__" + name } type rpcRequest struct { JSONRPC string `json:"jsonrpc"` ID int `json:"id,omitempty"` Method string `json:"method"` Params any `json:"params,omitempty"` } type rpcResponse struct { JSONRPC string `json:"jsonrpc"` ID int `json:"id"` Result json.RawMessage `json:"result"` Error *rpcError `json:"error"` } type rpcError struct { Code int `json:"code"` Message string `json:"message"` } func (e *rpcError) Error() string { return fmt.Sprintf("mcp rpc error %d: %s", e.Code, e.Message) } // Client is a Streamable HTTP connection to one remote MCP server. type Client struct { server string url string headers map[string]string http *http.Client mu sync.Mutex nextID int sessionID string } // New connects to a remote MCP endpoint and performs the initialize handshake. // headers are sent on every request (Authorization, custom API keys, …). func New(ctx context.Context, server, url string, headers map[string]string) (*Client, error) { c := &Client{ server: server, url: url, headers: headers, http: &http.Client{Timeout: 120 * time.Second}, } if err := c.initialize(ctx); err != nil { return nil, err } return c, nil } func (c *Client) initialize(ctx context.Context) error { if _, err := c.call(ctx, "initialize", map[string]any{ "protocolVersion": protocolVersion, "capabilities": map[string]any{}, "clientInfo": map[string]any{"name": "artex", "version": "0.2"}, }); err != nil { return err } return c.notify(ctx, "notifications/initialized", map[string]any{}) } // Tools lists the server's tools and adapts them to CoreTools. func (c *Client) Tools(ctx context.Context) ([]actool.CoreTool, error) { raw, err := c.call(ctx, "tools/list", map[string]any{}) if err != nil { return nil, err } var res struct { Tools []remoteTool `json:"tools"` } if err := json.Unmarshal(raw, &res); err != nil { return nil, err } out := make([]actool.CoreTool, 0, len(res.Tools)) for _, rt := range res.Tools { out = append(out, c.wrap(rt)) } return out, nil } type remoteTool struct { Name string `json:"name"` Description string `json:"description"` InputSchema map[string]any `json:"inputSchema"` } func (c *Client) wrap(rt remoteTool) actool.CoreTool { schema := rt.InputSchema if schema == nil { schema = map[string]any{"type": "object"} } full := ToolName(c.server, rt.Name) return actool.Build(actool.Spec{ Name: full, Description: rt.Description, Schema: schema, Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision { return permission.AskUser("call MCP tool " + full + "?") }, Run: func(ctx context.Context, in json.RawMessage, _ *actool.ToolContext) (actool.Result, error) { var args any if len(in) > 0 { _ = json.Unmarshal(in, &args) } raw, err := c.call(ctx, "tools/call", map[string]any{"name": rt.Name, "arguments": args}) if err != nil { return actool.Errorf("Error: " + err.Error()), nil } var res struct { Content []struct { Type string `json:"type"` Text string `json:"text"` } `json:"content"` IsError bool `json:"isError"` } if err := json.Unmarshal(raw, &res); err != nil { return actool.Errorf("Error: bad MCP response: " + err.Error()), nil } var text string for _, blk := range res.Content { text += blk.Text } return actool.Result{Content: []llm.ContentBlock{llm.TextBlock(text)}, IsError: res.IsError}, nil }, }) } // Call invokes one tool and returns the concatenated text of its content blocks // (ScopeSentry answers with a single JSON text block). Unlike the CoreTool wrapper // from Tools(), this bypasses the agent permission (AskUser) layer — it is meant // for backend batch jobs (e.g. asset sync) that call MCP tools programmatically. func (c *Client) Call(ctx context.Context, tool string, args any) (string, error) { raw, err := c.call(ctx, "tools/call", map[string]any{"name": tool, "arguments": args}) if err != nil { return "", err } var res struct { Content []struct { Type string `json:"type"` Text string `json:"text"` } `json:"content"` IsError bool `json:"isError"` } if err := json.Unmarshal(raw, &res); err != nil { return "", fmt.Errorf("bad MCP response: %w", err) } var text strings.Builder for _, blk := range res.Content { text.WriteString(blk.Text) } if res.IsError { return text.String(), fmt.Errorf("mcp tool %q error: %s", tool, text.String()) } return text.String(), nil } // Close terminates the MCP session best-effort (DELETE with the session id, per the // Streamable HTTP spec). Servers that don't track sessions simply ignore it. func (c *Client) Close() error { c.mu.Lock() sid := c.sessionID c.mu.Unlock() if sid == "" { return nil } req, err := http.NewRequest(http.MethodDelete, c.url, nil) if err != nil { return nil } req.Header.Set("Mcp-Session-Id", sid) for k, v := range c.headers { req.Header.Set(k, v) } if resp, err := c.http.Do(req); err == nil { resp.Body.Close() } return nil } // --- JSON-RPC over Streamable HTTP --- func (c *Client) call(ctx context.Context, method string, params any) (json.RawMessage, error) { c.mu.Lock() c.nextID++ id := c.nextID c.mu.Unlock() resp, err := c.roundTrip(ctx, rpcRequest{JSONRPC: "2.0", ID: id, Method: method, Params: params}, true) if err != nil { return nil, err } if resp == nil { return nil, fmt.Errorf("mcp: empty response for %s", method) } if resp.Error != nil { return nil, resp.Error } return resp.Result, nil } func (c *Client) notify(ctx context.Context, method string, params any) error { _, err := c.roundTrip(ctx, rpcRequest{JSONRPC: "2.0", Method: method, Params: params}, false) return err } // roundTrip POSTs one JSON-RPC frame. When expectResp is false (a notification) the // server replies 202 Accepted with no body. Otherwise the reply is parsed from JSON // or from an SSE stream, whichever the server chose. func (c *Client) roundTrip(ctx context.Context, body rpcRequest, expectResp bool) (*rpcResponse, error) { data, err := json.Marshal(body) if err != nil { return nil, err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url, bytes.NewReader(data)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json, text/event-stream") req.Header.Set("MCP-Protocol-Version", protocolVersion) for k, v := range c.headers { req.Header.Set(k, v) } c.mu.Lock() sid := c.sessionID c.mu.Unlock() if sid != "" { req.Header.Set("Mcp-Session-Id", sid) } resp, err := c.http.Do(req) if err != nil { return nil, err } defer resp.Body.Close() // Capture the session id the server assigns on initialize. if sid == "" { if got := resp.Header.Get("Mcp-Session-Id"); got != "" { c.mu.Lock() c.sessionID = got c.mu.Unlock() } } if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, fmt.Errorf("mcp http %d: %s", resp.StatusCode, readSnippet(resp.Body)) } if !expectResp { return nil, nil // notification — no JSON-RPC body to parse } if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") { return parseSSE(resp.Body, body.ID) } var out rpcResponse if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { return nil, fmt.Errorf("mcp: decode json response: %w", err) } return &out, nil } // parseSSE reads an SSE stream and returns the first data frame that is a JSON-RPC // response matching wantID (server-to-client requests/notifications are skipped). func parseSSE(r io.Reader, wantID int) (*rpcResponse, error) { sc := bufio.NewScanner(r) sc.Buffer(make([]byte, 0, 64*1024), 8*1024*1024) var dataBuf strings.Builder flush := func() (*rpcResponse, bool) { if dataBuf.Len() == 0 { return nil, false } payload := dataBuf.String() dataBuf.Reset() var out rpcResponse if err := json.Unmarshal([]byte(payload), &out); err != nil { return nil, false } if out.ID != wantID { return nil, false } return &out, true } for sc.Scan() { line := sc.Text() if line == "" { // event boundary if resp, ok := flush(); ok { return resp, nil } continue } if strings.HasPrefix(line, "data:") { dataBuf.WriteString(strings.TrimSpace(line[len("data:"):])) } } if resp, ok := flush(); ok { return resp, nil } if err := sc.Err(); err != nil { return nil, err } return nil, fmt.Errorf("mcp: no matching response in event stream") } func readSnippet(r io.Reader) string { b, _ := io.ReadAll(io.LimitReader(r, 2048)) return strings.TrimSpace(string(b)) }