Skip to content

Add client examples for streamable http #367

@rdmcguire

Description

@rdmcguire

It would be useful to have a client example for connecting to a streamable http MCP server. A few items that would be great to include:

  • TLS
  • Auth / Custom Headers
  • Middelware

While my basic usage may be wrong, I couldn't find a way to perform even a basic operation using StreamableClientTransport without adding a RoundTripper to add req.Header.Set("Accept", "text/event-stream,application/json").

This seems like something the client should handle, so perhaps I'm using the package wrong (thus the thought that examples may help).

Here is my current (presumably incorrect) usage, which required the roundtripper to make any calls:

func (a *AIWorkerMCP) TestGitToolHandler(ctx context.Context) (*mcp.CallToolResult, error) {
	client := mcp.NewClient(AIWorkerMCPImpl, &mcp.ClientOptions{})

	session, err := client.Connect(ctx, &mcp.StreamableClientTransport{
		Endpoint:   a.getHost() + "/api/mcp/",
		HTTPClient: customHTTPClient,
	}, nil)
	if err != nil {
		return nil, err
	}
	defer session.Close()

	return session.CallTool(ctx, &mcp.CallToolParams{
		Name: "aiworkermcp-git",
		Arguments: &GitToolArgs{
			Repo:         "git@someremote:somerepo.git",
			SourceBranch: "development",
			TargetBranch: "main",
		},
	})
}

type acceptHeaderRoundTripper struct {
	delegate http.RoundTripper
}

func (rt *acceptHeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	req.Header.Set("Accept", "text/event-stream,application/json")
	return rt.delegate.RoundTrip(req)
}

var customHTTPClient = &http.Client{
	Transport: &acceptHeaderRoundTripper{
		delegate: http.DefaultTransport,
	},
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedGood candidate for contribution. Comment first to say you're working on it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions