Skip to content

feat: support user-defined fallback protocols array#707

Open
anubhav756 wants to merge 13 commits into
anubhav-mcp-draftfrom
anubhav-custom-protocols
Open

feat: support user-defined fallback protocols array#707
anubhav756 wants to merge 13 commits into
anubhav-mcp-draftfrom
anubhav-custom-protocols

Conversation

@anubhav756

Copy link
Copy Markdown
Contributor

Description

This PR introduces the ability for devs to provide a custom array of preferred protocol versions when initializing the client, rather than being forced to rely on the default SDK fallback order.

Note

This PR is stacked on top of #703.

Changes:

  • Custom Protocols Array:
    • Adds an optional supported_protocols list parameter to the ToolboxClient.
  • Intersection & Sorting Logic:
    • When the user provides a custom protocol array, the client computes the intersection between the user's requested versions and the SDK's natively supported versions.
    • The resulting list is strictly sorted in descending chronological order (newest to oldest).
  • Unit Testing (test_client.py):
    • Added dedicated unit tests to verify the custom array initialization.
    • Added assertions to verify that arrays passed in scrambled or reversed chronological order are correctly sanitized and sorted before the transport layer attempts negotiation.

@anubhav756 anubhav756 force-pushed the anubhav-custom-protocols branch from 4a5deab to 67ae062 Compare July 2, 2026 06:18
Comment thread packages/toolbox-core/README.md Outdated
@anubhav756 anubhav756 force-pushed the anubhav-custom-protocols branch from 9fc5bd7 to 4ba1f82 Compare July 2, 2026 06:25
@anubhav756 anubhav756 force-pushed the anubhav-custom-protocols branch from 4ba1f82 to da892a4 Compare July 2, 2026 06:26
Comment thread packages/toolbox-core/README.md
@anubhav756 anubhav756 requested a review from twishabansal July 2, 2026 06:49

def test_toolbox_client_custom_protocols_invalid():
"""Test that custom protocols array raises error on invalid inputs."""
import pytest

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: pytest is also exported at the top of the file

should typically be managed externally.
client_headers: Headers to include in each request sent through this
client.
protocol: The communication protocol to use.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the docstring with info about protocol?

Comment on lines +198 to +223
if isinstance(protocol, list):
if not protocol:
raise ValueError("protocol list cannot be empty")
user_protocols = [
p.value if isinstance(p, Protocol) else str(p) for p in protocol
]

supported_mcp_versions = Protocol.get_supported_mcp_versions()
for p in user_protocols:
if p not in supported_mcp_versions:
raise ValueError(
f"Invalid protocol version '{p}'. Must be one of: {supported_mcp_versions}"
)

user_protocols_set = set(user_protocols)
# Intersect with the globally sorted list to strictly enforce newest-to-oldest ordering
supported_protocols = [
v for v in supported_mcp_versions if v in user_protocols_set
]
if not supported_protocols:
raise ValueError("No supported protocols found in the provided list")
initial_protocol = Protocol(supported_protocols[0])
else:
supported_protocols = None
initial_protocol = protocol

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to document this new behaviour?

Eg.
protocol=Protocol.MCP_LATESTsupported_protocols=None → transport falls back through all older versions during negotiation.

protocol=[Protocol.MCP_LATEST]supported_protocols =["2025-11-25"] → negotiation is restricted to that one version, no fallback.

Also that the newest version in the list is negotiated for at first

supported_protocols = [
v for v in supported_mcp_versions if v in user_protocols_set
]
if not supported_protocols:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every user_protocols entry is already validated to be insupported_mcp_versions, and the list is non-empty, so the intersection can never be empty here.

Seems like unreachable code. Should we remove this?

@anubhav756 anubhav756 changed the title feat(core): support user-defined fallback protocols array feat: support user-defined fallback protocols array Jul 2, 2026
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.

3 participants