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
|
|
@ -813,6 +813,8 @@ func (s *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
|
|||
return nil
|
||||
}
|
||||
|
||||
// postMessage POSTs msg to the server and reads the response.
|
||||
// It returns the session ID from the response.
|
||||
func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string, msg jsonrpc.Message) (string, error) {
|
||||
data, err := jsonrpc2.EncodeMessage(msg)
|
||||
if err != nil {
|
||||
|
|
@ -849,9 +851,17 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
|
|||
// Section 2.1: The SSE stream is initiated after a POST.
|
||||
go s.handleSSE(resp)
|
||||
case "application/json":
|
||||
// TODO: read the body and send to s.incoming (in a select that also receives from s.done).
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
return "", fmt.Errorf("streamable HTTP client does not yet support raw JSON responses")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
select {
|
||||
case s.incoming <- body:
|
||||
case <-s.done:
|
||||
// The connection was closed by the client; exit gracefully.
|
||||
}
|
||||
return sessionID, nil
|
||||
default:
|
||||
resp.Body.Close()
|
||||
return "", fmt.Errorf("unsupported content type %q", ct)
|
||||
|
|
|
|||
Loading…
Reference in a new issue