Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions internal/mcpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ import (
// Client is a minimal Streamable HTTP MCP client that sends JSON-RPC 2.0
// tool-call requests to an MCP gateway and parses the response.
type Client struct {
url string
hdrs map[string]string
http *http.Client
mu sync.Mutex
nextID int64
init bool
url string
hdrs map[string]string
http *http.Client
mu sync.Mutex
nextID int64
init bool
initGate chan struct{}
}

func New(url string, headers map[string]string) *Client {
return &Client{
url: strings.TrimRight(url, "/"),
hdrs: headers,
http: &http.Client{Timeout: 60 * time.Second},
url: strings.TrimRight(url, "/"),
hdrs: headers,
http: &http.Client{Timeout: 60 * time.Second},
initGate: make(chan struct{}, 1),
}
}

Expand Down Expand Up @@ -102,7 +104,10 @@ func (c *Client) CallTool(name string, args map[string]any) (string, error) {

// CallToolContext is CallTool with a caller-supplied deadline/cancellation.
func (c *Client) CallToolContext(ctx context.Context, name string, args map[string]any) (string, error) {
if err := c.ensureInit(); err != nil {
if ctx == nil {
ctx = context.Background()
}
if err := c.ensureInit(ctx); err != nil {
return "", fmt.Errorf("mcp init: %w", err)
}
c.mu.Lock()
Expand Down Expand Up @@ -206,7 +211,10 @@ func (c *Client) ListTools() ([]Tool, error) {

// ListToolsContext is ListTools with a caller-supplied deadline/cancellation.
func (c *Client) ListToolsContext(ctx context.Context) ([]Tool, error) {
if err := c.ensureInit(); err != nil {
if ctx == nil {
ctx = context.Background()
}
if err := c.ensureInit(ctx); err != nil {
return nil, fmt.Errorf("mcp init: %w", err)
}
c.mu.Lock()
Expand Down Expand Up @@ -264,14 +272,23 @@ func (c *Client) ListToolsContext(ctx context.Context) ([]Tool, error) {
return result.Tools, nil
}

func (c *Client) ensureInit() error {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Client) ensureInit(ctx context.Context) error {
select {
case c.initGate <- struct{}{}:
defer func() { <-c.initGate }()
case <-ctx.Done():
return ctx.Err()
}
if err := ctx.Err(); err != nil {
return err
}
if c.init {
return nil
}
c.mu.Lock()
c.nextID++
id := c.nextID
c.mu.Unlock()

req := rpcRequest{
JSONRPC: "2.0",
Expand All @@ -287,7 +304,7 @@ func (c *Client) ensureInit() error {
},
}
body, _ := json.Marshal(req)
httpReq, err := http.NewRequest("POST", c.url, bytes.NewReader(body))
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.url, bytes.NewReader(body))
if err != nil {
return err
}
Expand All @@ -298,12 +315,18 @@ func (c *Client) ensureInit() error {
}
resp, err := c.http.Do(httpReq)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
c.init = true
return nil
}
defer resp.Body.Close()
raw, err := c.readResponse(resp)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
c.init = true
return nil
}
Expand All @@ -312,14 +335,16 @@ func (c *Client) ensureInit() error {
// Non-fatal: continue even if init fails.
}

c.mu.Lock()
c.nextID++
c.mu.Unlock()
notifReq := rpcRequest{
JSONRPC: "2.0",
ID: 0,
Method: "notifications/initialized",
}
notifBody, _ := json.Marshal(notifReq)
notifHTTP, _ := http.NewRequest("POST", c.url, bytes.NewReader(notifBody))
notifHTTP, _ := http.NewRequestWithContext(ctx, "POST", c.url, bytes.NewReader(notifBody))
notifHTTP.Header.Set("Content-Type", "application/json")
notifHTTP.Header.Set("Accept", "application/json, text/event-stream")
for k, v := range c.hdrs {
Expand All @@ -329,6 +354,9 @@ func (c *Client) ensureInit() error {
if err == nil {
notifResp.Body.Close()
}
if err := ctx.Err(); err != nil {
return err
}
c.init = true
return nil
}
Expand Down
60 changes: 60 additions & 0 deletions internal/mcpclient/init_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package mcpclient

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

func TestMCPInitializationHonorsCallerCancellation(t *testing.T) {
var started atomic.Int32
release := make(chan struct{})
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req rpcRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "bad fixture request", http.StatusBadRequest)
return
}
switch req.Method {
case "initialize":
if started.Add(1) == 1 {
<-release
}
writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{}}`, req.ID))
case "notifications/initialized":
w.WriteHeader(http.StatusAccepted)
default:
writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"tools":[]}}`, req.ID))
}
}))
defer endpoint.Close()
defer close(release)
client := New(endpoint.URL, nil)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Millisecond)
defer cancel()
result := make(chan error, 1)
go func() {
_, err := client.ListToolsContext(ctx)
result <- err
}()
select {
case err := <-result:
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("want caller deadline, got %v", err)
}
case <-time.After(time.Second):
t.Fatal("initialization ignored the caller deadline")
}
if _, err := client.ListTools(); err != nil {
t.Fatalf("fresh caller cannot retry initialization: %v", err)
}
if started.Load() != 2 {
t.Fatalf("want two initialization requests, got %d", started.Load())
}
}