| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"flag" |
| 9 |
"log" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"os" |
| 13 |
"reflect" |
| 14 |
"runtime" |
| 15 |
"runtime/pprof" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/google/jsonschema-go/jsonschema" |
| 20 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 21 |
) |
| 22 |
|
| 23 |
func BenchmarkStreamableServing(b *testing.B) { |
| 24 |
|
| 25 |
|
| 26 |
customSchemas := map[reflect.Type]*jsonschema.Schema{ |
| 27 |
reflect.TypeFor[Probability](): {Type: "number", Minimum: jsonschema.Ptr(0.0), Maximum: jsonschema.Ptr(1.0)}, |
| 28 |
reflect.TypeFor[WeatherType](): {Type: "string", Enum: []any{Sunny, PartlyCloudy, Cloudy, Rainy, Snowy}}, |
| 29 |
} |
| 30 |
opts := &jsonschema.ForOptions{TypeSchemas: customSchemas} |
| 31 |
in, err := jsonschema.For[WeatherInput](opts) |
| 32 |
if err != nil { |
| 33 |
b.Fatal(err) |
| 34 |
} |
| 35 |
out, err := jsonschema.For[WeatherOutput](opts) |
| 36 |
if err != nil { |
| 37 |
b.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 41 |
mcp.AddTool(server, &mcp.Tool{ |
| 42 |
Name: "weather", |
| 43 |
InputSchema: in, |
| 44 |
OutputSchema: out, |
| 45 |
}, WeatherTool) |
| 46 |
|
| 47 |
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server { |
| 48 |
return server |
| 49 |
}, &mcp.StreamableHTTPOptions{JSONResponse: true}) |
| 50 |
httpServer := httptest.NewServer(handler) |
| 51 |
defer httpServer.Close() |
| 52 |
|
| 53 |
ctx := b.Context() |
| 54 |
session, err := mcp.NewClient(testImpl, nil).Connect(ctx, &mcp.StreamableClientTransport{Endpoint: httpServer.URL}, nil) |
| 55 |
if err != nil { |
| 56 |
b.Fatal(err) |
| 57 |
} |
| 58 |
defer session.Close() |
| 59 |
b.ResetTimer() |
| 60 |
for range b.N { |
| 61 |
if _, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 62 |
Name: "weather", |
| 63 |
Arguments: WeatherInput{ |
| 64 |
Location: Location{Name: "somewhere"}, |
| 65 |
Days: 7, |
| 66 |
}, |
| 67 |
}); err != nil { |
| 68 |
b.Errorf("CallTool failed: %v", err) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
var streamableHeap = flag.String("streamable_heap", "", "if set, write streamable heap profiles with this prefix") |
| 74 |
|
| 75 |
func BenchmarkStreamableServing_BadSessions(b *testing.B) { |
| 76 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 77 |
|
| 78 |
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server { |
| 79 |
return server |
| 80 |
}, &mcp.StreamableHTTPOptions{JSONResponse: true}) |
| 81 |
|
| 82 |
httpServer := httptest.NewServer(handler) |
| 83 |
defer httpServer.Close() |
| 84 |
|
| 85 |
ctx := b.Context() |
| 86 |
|
| 87 |
if *streamableHeap != "" { |
| 88 |
writeHeap := func(file string) { |
| 89 |
|
| 90 |
runtime.GC() |
| 91 |
runtime.GC() |
| 92 |
f, err := os.Create(file) |
| 93 |
if err != nil { |
| 94 |
log.Fatal("could not create memory profile: ", err) |
| 95 |
} |
| 96 |
defer func() { |
| 97 |
if err := f.Close(); err != nil { |
| 98 |
b.Errorf("writing heap file %q: %v", file, err) |
| 99 |
} |
| 100 |
}() |
| 101 |
if err := pprof.Lookup("heap").WriteTo(f, 0); err != nil { |
| 102 |
b.Errorf("could not write heap profile: %v", err) |
| 103 |
} |
| 104 |
} |
| 105 |
writeHeap(*streamableHeap + ".before") |
| 106 |
defer writeHeap(*streamableHeap + ".after") |
| 107 |
} |
| 108 |
|
| 109 |
b.ResetTimer() |
| 110 |
for range b.N { |
| 111 |
req, err := http.NewRequestWithContext(ctx, http.MethodPost, httpServer.URL, strings.NewReader("{}")) |
| 112 |
if err != nil { |
| 113 |
b.Fatal(err) |
| 114 |
} |
| 115 |
req.Header.Add("Accept", "application/json") |
| 116 |
req.Header.Add("Accept", "text/event-stream") |
| 117 |
resp, err := http.DefaultClient.Do(req) |
| 118 |
if err != nil { |
| 119 |
b.Fatal(err) |
| 120 |
} |
| 121 |
if got, want := resp.StatusCode, http.StatusBadRequest; got != want { |
| 122 |
b.Fatalf("POST got status %d, want %d", got, want) |
| 123 |
} |
| 124 |
if got := resp.Header.Get("Mcp-Session-Id"); got != "" { |
| 125 |
b.Fatalf("POST got unexpected session ID") |
| 126 |
} |
| 127 |
resp.Body.Close() |
| 128 |
} |
| 129 |
} |