Skip to content

feat(transport-security): add subdomain wildcard support for allowed_hosts#2144

Open
Chenzhen-Ye wants to merge 1 commit intomodelcontextprotocol:mainfrom
Chenzhen-Ye:feature/2141-allowed-hosts-subdomain-wildcard
Open

feat(transport-security): add subdomain wildcard support for allowed_hosts#2144
Chenzhen-Ye wants to merge 1 commit intomodelcontextprotocol:mainfrom
Chenzhen-Ye:feature/2141-allowed-hosts-subdomain-wildcard

Conversation

@Chenzhen-Ye
Copy link

@Chenzhen-Ye Chenzhen-Ye commented Feb 25, 2026

TransportSecuritySettings.allowed_hosts now supports *.domain patterns (e.g. *.mysite.com) so that a single entry can allow the base domain and any subdomain (app.mysite.com, api.mysite.com, etc.) instead of listing each host explicitly. This makes multi-subdomain or dynamic subdomain setups practical.

  • Add _hostname_from_host() to strip port from Host header (including IPv6)
  • In _validate_host(), treat entries starting with *. as subdomain
    wildcards: match hostname equal to base domain or ending with .
  • Preserve existing behaviour: exact match and example.com:* port wildcard
  • Document the three pattern types in allowed_hosts docstring
  • Add integration tests for SSE and StreamableHTTP with *.mysite.com

Github-Issue: #2141

Motivation and Context

Closes #2141

How Has This Been Tested?

Integration tests (pytest)
SSE (test_sse_security_subdomain_wildcard_host): Server started with allowed_hosts=[".mysite.com", "127.0.0.1:"]. Verified that requests with Host: app.mysite.com, api.mysite.com, and mysite.com return 200, and Host: other.com returns 421 with "Invalid Host header".
StreamableHTTP (test_streamable_http_security_subdomain_wildcard_host): Same allowed_hosts. Verified that a POST with Host: app.mysite.com returns 200 and with Host: other.com returns 421.

Regression
All existing transport/SSE/StreamableHTTP security tests were run (26 tests in test_sse_security.py and test_streamable_http_security.py); no existing behaviour was changed.

Not tested in a real application
This was only validated via the test suite; no manual runs in a live app or multi-subdomain deployment.

Breaking Changes

No

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

@Chenzhen-Ye Chenzhen-Ye force-pushed the feature/2141-allowed-hosts-subdomain-wildcard branch 3 times, most recently from c3d8639 to 3f9a2d1 Compare February 25, 2026 09:02
…hosts

TransportSecuritySettings.allowed_hosts now supports *.domain patterns
(e.g. *.mysite.com) so that a single entry can allow the base domain and
any subdomain (app.mysite.com, api.mysite.com, etc.) instead of listing
each host explicitly. This makes multi-subdomain or dynamic subdomain
setups practical.

- Add _hostname_from_host() to strip port from Host header (including IPv6)
- In _validate_host(), treat entries starting with *. as subdomain
  wildcards: match hostname equal to base domain or ending with .<base>
- Preserve existing behaviour: exact match and example.com:* port wildcard
- Document the three pattern types in allowed_hosts docstring
- Add integration tests for SSE and StreamableHTTP with *.mysite.com

Github-Issue: modelcontextprotocol#2141
@Chenzhen-Ye Chenzhen-Ye changed the title feat(transport-security): add subdomain wildcard support for allowed_… feat(transport-security): add subdomain wildcard support for allowed_hosts Feb 26, 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.

Add wildcard pattern support for allowed_hosts in transport security

1 participant