mcp-agent icon indicating copy to clipboard operation
mcp-agent copied to clipboard

Passing authorization header to an MCP server over websockets

Open pmohan6 opened this issue 8 months ago • 5 comments

Hi there,

I would like to add websockets support to the library. I think I have the changes ready. But for my usecase, I would also need to pass in an Authorization header with a Bearer token to the server for auth. Is that currently possible? If not, what's the best way to do that?

Thanks for your help!

pmohan6 avatar Mar 28 '25 03:03 pmohan6

@pmohan6 thank you for preparing the contribution -- websocket support will be super to add! My suggestion would be to allow the token to be specified in the mcp_agent config (which can be passed programmatically or via mcp_agent.config.yaml / mcp_agent.secrets.yaml)

We have MCPServerAuthSettings which can be configured as the auth property on the server's settings:

https://github.com/lastmile-ai/mcp-agent/blob/main/src/mcp_agent/config.py#L13

Perhaps we add a field there? Alternatively, we could add a more general "headers" field to MCPServerSettings to allow broader configuration.

https://github.com/lastmile-ai/mcp-agent/blob/main/src/mcp_agent/config.py#L44

In general I was envisioning this similar to how we allow env to be configured (see the Slack example: https://github.com/lastmile-ai/mcp-agent/blob/main/examples/mcp_basic_slack_agent/mcp_agent.secrets.yaml.example)

Lastly, for completeness, there is a register_init_hook on the ServerRegistry class that could also be used for this purpose for more dynamic initialization. https://github.com/lastmile-ai/mcp-agent/blob/main/src/mcp_agent/mcp_server_registry.py#L229 -- I added that for things like authentication, but haven't seen a use case for it yet. Sharing in case that's helpful, but if we can accomplish this with the config we should opt for that.

I'd also be grateful if you contributed a websocket example to the examples! Let me know how I can help!

saqadri avatar Mar 28 '25 10:03 saqadri

Thanks for the detailed response @saqadri ! I think I'll come back to the auth stuff in a bit as that requirements may have cleared for me. But wrt supporting websockets, it looks like the protocol may be moving more towards streamable HTTP. Would you still care for support for websockets or let it be?

I'm happy to submit a PR if you'd like regardless. Are there any instructions on how to run tests?

https://github.com/modelcontextprotocol/specification/discussions/102 https://github.com/modelcontextprotocol/specification/pull/206

pmohan6 avatar Mar 29 '25 00:03 pmohan6

@pmohan6 it would be great to submit a PR! I can help you test it out and also I am working on automated tests, so can include this in that. For the time being if you could add an example of using it in the examples directory as part of your PR, that'll be great!

saqadri avatar Mar 29 '25 22:03 saqadri

@saqadri - added support for websocket and an example here: https://github.com/lastmile-ai/mcp-agent/pull/114

Will work on passing auth soon.

pmohan6 avatar Apr 06 '25 00:04 pmohan6

Ignore the last comment. Added a new PR with both websockets and auth here: https://github.com/lastmile-ai/mcp-agent/pull/116. Wanted to raise a stacked PR but I don't have write permissions on this repo to create new branches, so both features are in a single PR.

pmohan6 avatar Apr 06 '25 05:04 pmohan6

Closing this issue as completed -- thank you again for the contribution, @pmohan6!

saqadri avatar May 06 '25 02:05 saqadri

Np! Happy to help.

pmohan6 avatar May 06 '25 04:05 pmohan6