Skip to content

Commit b9a56bf

Browse files
committed
fix: add required Accept header for streamable HTTP transport
When connecting to MCP servers via streamable HTTP, mcptools was not setting the Accept header. Some servers like mcp.grep.app require clients to accept both application/json and text/event-stream, and return 405 errors when this header is missing. This change ensures the Accept header is always set for both HTTP and SSE transports, fixing compatibility with servers that enforce this requirement. Tested with mcp.grep.app and existing unit tests pass.
1 parent 3666180 commit b9a56bf

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

cmd/mcptools/commands/utils.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,24 @@ var CreateClientFunc = func(args []string, _ ...client.ClientOption) (*client.Cl
116116
return nil, fmt.Errorf("failed to parse authentication: %w", authErr)
117117
}
118118

119-
// Create headers map if authentication is provided
119+
// Create headers map with required Accept header for MCP protocol
120120
headers := make(map[string]string)
121+
122+
// Add authentication header if provided
121123
if authHeader != "" {
122124
headers["Authorization"] = authHeader
123125
}
124126

127+
// Add Accept header required by MCP streamable HTTP and SSE transports
128+
// Many MCP servers require clients to accept both JSON responses and event streams
129+
headers["Accept"] = "application/json, text/event-stream"
130+
125131
if TransportOption == "sse" {
126132
// For SSE transport, use transport.ClientOption
127-
if len(headers) > 0 {
128-
c, err = client.NewSSEMCPClient(cleanURL, transport.WithHeaders(headers))
129-
} else {
130-
c, err = client.NewSSEMCPClient(cleanURL)
131-
}
133+
c, err = client.NewSSEMCPClient(cleanURL, transport.WithHeaders(headers))
132134
} else {
133135
// For StreamableHTTP transport, use transport.StreamableHTTPCOption
134-
if len(headers) > 0 {
135-
c, err = client.NewStreamableHttpClient(cleanURL, transport.WithHTTPHeaders(headers))
136-
} else {
137-
c, err = client.NewStreamableHttpClient(cleanURL)
138-
}
136+
c, err = client.NewStreamableHttpClient(cleanURL, transport.WithHTTPHeaders(headers))
139137
}
140138

141139
if err != nil {

0 commit comments

Comments
 (0)