Skip to content

feat: add CORS support to registry API#711

Merged
domdomegg merged 5 commits intomodelcontextprotocol:mainfrom
Ironstead-Group:fix/cors
Oct 27, 2025
Merged

feat: add CORS support to registry API#711
domdomegg merged 5 commits intomodelcontextprotocol:mainfrom
Ironstead-Group:fix/cors

Conversation

@ironsteadlabs
Copy link
Contributor

Fixes #710

This PR adds CORS middleware to enable browser-based clients and web applications to access the registry API.

Changes

  • Add CORSMiddleware in internal/api/cors.go to handle preflight OPTIONS requests and inject CORS headers
  • Integrate CORS middleware into the server middleware stack
  • Add CORS configuration options (CORS_ENABLED and CORS_ALLOWED_ORIGIN)

Testing

  • All existing tests pass
  • Manual testing confirms CORS headers are present in responses
  • OPTIONS preflight requests now return 204 with proper headers

Configuration

New environment variables:

  • MCP_REGISTRY_CORS_ENABLED (default: true)
  • MCP_REGISTRY_CORS_ALLOWED_ORIGIN (default: *)

Add Cross-Origin Resource Sharing (CORS) middleware to enable browser-based
clients and web applications to access the registry API.

Changes:
- Add CORSMiddleware in internal/api/cors.go to handle preflight OPTIONS
  requests and inject CORS headers into all responses
- Integrate CORS middleware into the server middleware stack in
  internal/api/server.go between TrailingSlashMiddleware and the mux
- Add CORS configuration options to internal/config/config.go:
  - CORS_ENABLED (default: true) to enable/disable CORS
  - CORS_ALLOWED_ORIGIN (default: *) to configure allowed origins

The middleware adds the following headers to all responses:
- Access-Control-Allow-Origin
- Access-Control-Allow-Methods
- Access-Control-Allow-Headers
- Access-Control-Expose-Headers
- Access-Control-Max-Age

This resolves issues where browser-based tools, VS Code extensions, and
web applications were unable to query the registry API due to browser
security policies blocking cross-origin requests.
Copy link
Member

@domdomegg domdomegg left a comment

Choose a reason for hiding this comment

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

Thanks, this generally LGTM.

Can we:

  • Add tests
  • Remove the config option: prefer convention > configuration

Also would have a slight preference to a well-used cors library if there is one in Golang, but happy with this.

@ironsteadlabs
Copy link
Contributor Author

Thanks for the feedback! I've updated the PR to address all the comments:

Changes made:

  • Added tests - Created comprehensive CORS tests in internal/api/cors_test.go covering GET, POST, PUT, DELETE, and OPTIONS preflight requests
  • Removed config options - Eliminated MCP_REGISTRY_CORS_ENABLED and MCP_REGISTRY_CORS_ALLOWED_ORIGIN environment variables. CORS is now always enabled with sensible defaults (convention over configuration)
  • Using rs/cors library - Replaced the custom implementation with github.com/rs/cors, which provides battle-tested CORS handling

CORS Configuration:

  • Allows all origins (*)
  • Supports all standard HTTP methods (GET, POST, PUT, DELETE, OPTIONS)
  • Allows all headers
  • 24-hour preflight cache
  • No credentials (required when using wildcard origin)

All tests pass with make check. Ready for re-review!

@domdomegg
Copy link
Member

Thanks! I don't seem to be able to see the changes you mentioned in your most recent comment - are you sure you've pushed them to this branch?

Replace custom CORS middleware with rs/cors library and remove
configuration options in favor of convention over configuration.

- Add rs/cors dependency
- Remove custom CORS implementation
- Remove CORS config options from config.go
- Configure CORS with permissive settings for public API
- Add CORS tests
@ironsteadlabs
Copy link
Contributor Author

@domdomegg my bad, I updated from main yesterday and forgot to push the refactor commit from local to remote. Noticed a test failing after the pull from main today so I just resolved that issue too.

Copy link
Member

@domdomegg domdomegg left a comment

Choose a reason for hiding this comment

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

Awesome, thanks! 🙌

@domdomegg domdomegg merged commit 73d2174 into modelcontextprotocol:main Oct 27, 2025
3 checks passed
Swathi-MuraliSrinivasan pushed a commit to Swathi-MuraliSrinivasan/paychex-mcp-registry that referenced this pull request Nov 20, 2025
Fixes modelcontextprotocol#710

This PR adds CORS middleware to enable browser-based clients and web
applications to access the registry API.

## Changes

- Add CORSMiddleware in internal/api/cors.go to handle preflight OPTIONS
requests and inject CORS headers
- Integrate CORS middleware into the server middleware stack
- Add CORS configuration options (CORS_ENABLED and CORS_ALLOWED_ORIGIN)

## Testing

- All existing tests pass
- Manual testing confirms CORS headers are present in responses
- OPTIONS preflight requests now return 204 with proper headers

## Configuration

New environment variables:
- MCP_REGISTRY_CORS_ENABLED (default: true)
- MCP_REGISTRY_CORS_ALLOWED_ORIGIN (default: *)
slimslenderslacks pushed a commit to slimslenderslacks/registry that referenced this pull request Dec 18, 2025
Fixes modelcontextprotocol#710

This PR adds CORS middleware to enable browser-based clients and web
applications to access the registry API.

- Add CORSMiddleware in internal/api/cors.go to handle preflight OPTIONS
requests and inject CORS headers
- Integrate CORS middleware into the server middleware stack
- Add CORS configuration options (CORS_ENABLED and CORS_ALLOWED_ORIGIN)

- All existing tests pass
- Manual testing confirms CORS headers are present in responses
- OPTIONS preflight requests now return 204 with proper headers

New environment variables:
- MCP_REGISTRY_CORS_ENABLED (default: true)
- MCP_REGISTRY_CORS_ALLOWED_ORIGIN (default: *)
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.

feat: add CORS support to registry API

2 participants