Skip to content

Conversation

@damianoneill
Copy link

@damianoneill damianoneill commented Oct 26, 2025

Implements per-request headers functionality to enable various use cases such as multi-tenant authentication, request tracing, A/B testing, and debugging while maintaining a single persistent connection.

Changes:

  • Add extra_headers parameter to ClientSession.call_tool()
  • Extend ClientMessageMetadata to support extra_headers
  • Update StreamableHTTPTransport to merge per-request headers
  • Add tests
  • Include usage examples and documentation
  • Maintain full backward compatibility

This addresses GitHub issues #1509 and supports multi-tenant scenarios where different requests require different authentication tokens or contextual headers.

Future work (as part of this PR) will extend extra_headers support to other ClientSession methods (get_prompt, read_resource, etc.) based on maintainer feedback.

Motivation and Context

Disscussed in issue #1509

How Has This Been Tested?

UTs, commit provided for review, is approach is agreed will proceed with extensive real world testing before finalising this PR.

Breaking Changes

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Implements per-request headers functionality to enable various use cases
such as multi-tenant authentication, request tracing, A/B testing, and
debugging while maintaining a single persistent connection.

Changes:
- Add extra_headers parameter to ClientSession.call_tool()
- Extend ClientMessageMetadata to support extra_headers
- Update StreamableHTTPTransport to merge per-request headers
- Add tests
- Include usage examples and documentation
- Maintain full backward compatibility

This addresses GitHub issues modelcontextprotocol#1509
and supports multi-tenant scenarios where different requests require
different authentication tokens or contextual headers.

Future work will extend extra_headers support to other ClientSession
methods (get_prompt, read_resource, etc.) based on maintainer feedback.
- Add 7 diverse usage examples covering tracing, A/B testing, debugging, auth, multi-tenancy
- Include multi-tenant example function demonstrating practical usage patterns
- Add proper error handling and realistic server endpoints
- Update code snippets via automated script for consistency
- Fix markdownlint formatting issues
@damianoneill damianoneill marked this pull request as draft October 26, 2025 10:37
@damianoneill damianoneill changed the title feat: add per-request HTTP headers support for call_tool() feat: add per-request HTTP headers support for client session methods Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant