mcp: support application/json in streamable client (#181)
The client handles POST responses with content type application/json. For: #10 Fixes #129
This commit is contained in:
parent
2b6f7b5185
commit
64b5b9125e
1 changed files with 59 additions and 3 deletions
|
|
@ -158,7 +158,8 @@ func TestClientReplay(t *testing.T) {
|
|||
client := NewClient(testImpl, &ClientOptions{
|
||||
ProgressNotificationHandler: func(ctx context.Context, cc *ClientSession, params *ProgressNotificationParams) {
|
||||
notifications <- params.Message
|
||||
}})
|
||||
},
|
||||
})
|
||||
clientSession, err := client.Connect(ctx, NewStreamableClientTransport(proxy.URL, nil))
|
||||
if err != nil {
|
||||
t.Fatalf("client.Connect() failed: %v", err)
|
||||
|
|
@ -678,6 +679,51 @@ func mustMarshal(t *testing.T, v any) json.RawMessage {
|
|||
return data
|
||||
}
|
||||
|
||||
func TestStreamableClientTransportApplicationJSON(t *testing.T) {
|
||||
// Test handling of application/json responses.
|
||||
ctx := context.Background()
|
||||
resp := func(id int64, result any, err error) *jsonrpc.Response {
|
||||
return &jsonrpc.Response{
|
||||
ID: jsonrpc2.Int64ID(id),
|
||||
Result: mustMarshal(t, result),
|
||||
Error: err,
|
||||
}
|
||||
}
|
||||
initResult := &InitializeResult{
|
||||
Capabilities: &serverCapabilities{
|
||||
Completions: &completionCapabilities{},
|
||||
Logging: &loggingCapabilities{},
|
||||
Tools: &toolCapabilities{ListChanged: true},
|
||||
},
|
||||
ProtocolVersion: latestProtocolVersion,
|
||||
ServerInfo: &Implementation{Name: "testServer", Version: "v1.0.0"},
|
||||
}
|
||||
initResp := resp(1, initResult, nil)
|
||||
|
||||
serverHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := jsonrpc2.EncodeMessage(initResp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
httpServer := httptest.NewServer(http.HandlerFunc(serverHandler))
|
||||
defer httpServer.Close()
|
||||
|
||||
transport := NewStreamableClientTransport(httpServer.URL, nil)
|
||||
client := NewClient(testImpl, nil)
|
||||
session, err := client.Connect(ctx, transport)
|
||||
if err != nil {
|
||||
t.Fatalf("client.Connect() failed: %v", err)
|
||||
}
|
||||
defer session.Close()
|
||||
if diff := cmp.Diff(initResult, session.initializeResult); diff != "" {
|
||||
t.Errorf("mismatch (-want, +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventID(t *testing.T) {
|
||||
tests := []struct {
|
||||
sid StreamID
|
||||
|
|
|
|||
Loading…
Reference in a new issue