modelcontextprotocol / modelcontextprotocol/python-sdk

Add wildcard pattern support for `allowed_hosts` in transport security

Open
#2,141 1 comment 0 reactions 1 assignee View on GitHub

@pcarleton is already working on this.

Since Mar 5, 2026.

enhancement needs confirmation
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 1h
Merged PRs (30d)
31

Description

Description

Problem

Currently, the allowed_hosts configuration in TransportSecuritySettings only supports exact string matching link. This is too restrictive for real-world scenarios where you need to allow multiple subdomains under the same parent domain.

For example, if you want to allow app.mysite.com, api.mysite.com, admin.mysite.com, etc., you currently need to list each subdomain explicitly:

allowed_hosts=["app.mysite.com", "api.mysite.com", "admin.mysite.com", ...]

This becomes impractical when you have many subdomains or dynamic subdomain generation.

Proposed Solution

Add wildcard pattern support similar to Starlette's TrustedHostMiddleware or Django's ALLOWED_HOSTS , allowing patterns like:

  • *.mysite.com - matches any subdomain of mysite.com (e.g., app.mysite.com, api.mysite.com) as well as the base domain mysite.com
  • example.com:* - matches example.com with any port (already supported)

Example Usage

from mcp.server.transport_security import TransportSecuritySettings

settings = TransportSecuritySettings(
    enable_dns_rebinding_protection=True,
    allowed_hosts=["*.mysite.com", "localhost:*"],
)

I'd be happy to follow up and create a PR.

References

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.