-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[Feature] Add mcp_server
to openbb_core
api
#7094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@piiq is this what you were looking for? 👀 |
Thanks for the PR, @MagnusS0, and great idea! Can you look at incorporating this, either as an extra here, as a keyword argument in the A similar pattern can be established where the FastAPI instance is being imported from Some degree of separation is going to be desirable and we'll need to expose the complete configurations for this server somewhere, potentially under, "system_settings:mcp_settings". The BTW this, |
Hi @MagnusS0 this indeed is a nice addition. I can only second @deeleeramone that it would be better if this is a standalone extension or a part of the I've taken a look at the fastapi-mcp package and apart from the mcp itself it does not seem to pull any new dependencies (which is a good thing) Regarding the problem of the server not shutting down - I thing it is going to be best to debug this once we separate the mcp from the openbb_core codebase. It would make it easier to focus. Thanks for the PR and let us know if you need any guidance |
Thanks for the feedback. I'll look at how to implement it as an extension. Btw, does all endpoints have operation IDs? I noticed it's used to get schemas for widgets here. Might be able to use this as an another layer of filtering before the agent gets the tools. Fetching all equity tools at once for example cost a lot of tokens. So reducing this to just a specific needed operations would be great. |
Operation IDs are assigned when |
Just an update on this, I have a master thesis to deliver in 10 days so will probably not contribute anything before after that. In case someone else wants to give it a go 🚀 |
Good luck with your thesis. This PR will be waiting for you |
1a4b979
to
6d09ead
Compare
@piiq @deeleeramone I have now made the MCP as a standalone extension, and added a new MCPSettings following how the OpenBB Platform API settings are set up. Let me know if this is closer to what you had in mind and any feedback. I have tested with GPT-4.1 and it is surprisingly good at finding the right tools, with minimal instructions 😄 Know limitations: |
Fwiw this worked for me - good job @MagnusS0 !!! But ya, I wonder if the library that @deeleeramone suggested is more indicated! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @MagnusS0, sorry for the delay, just got my hands on this.
Thanks again for the thoughtful work. I wanted to flag a few points that could help make this extension more future-proof and reduce maintenance overhead:
- HTTP+SSE deprecation in MCP
Thefastapi-mcp
package currently uses HTTP+SSE as the transport layer, but SSE has been deprecated by the MCP spec in favor of Streamable HTTP.
This is already being tracked as an open issue in thefastapi-mcp
repo (tadata-org/fastapi_mcp#61), but it’s been somewhat stale. Given that the repo itself hasn’t seen much activity during last month apart from promotional README edits, I’d be cautious about depending on it long-term. A different library, pointed out by @deeleeramone (https://github.com/jlowin/fastmcp) seems to be both actively maintained, implements all transport layers of the protocol and provides the tooling to spin up the mcp server from an existing FastAPI instance (or from an openapi spec). This migration is worth exploring, especially since this could also address the issue you mention in the PR description related to the server failing to shut down due to lingering SSE connections. - Global tool state = single-user design
The current behavior of tool activation being global to all connected agents effectively makes this a single-user server. That’s fine, but it should probably be made explicit in the docs so that users/integrators know not to deploy this for shared use in teams or networks without proper isolation. Long-term, a per-session or per-agent context might be needed, but that’s a bigger lift. MCPSettings
placement
Currently, theMCPSettings
are part ofSystemSettings
, which requires users to modify their globalsystem_settings.json
to enable the extension. I’d recommend scoping all configuration for this extension to the extension itself, so it remains fully self-contained. If a user installs the platform without the MCP extension, they shouldn’t have to deal with leftover configuration or errors related to unused settings.- Use
argparse
instead ofclick
Inmain.py
, the CLI entrypoint uses click to parse arguments. While this works and doesn’t introduce a new dependency,click
isn’t used anywhere else in the OpenBB's codebase. For consistency and maintainability, it would be better to useargparse
, which is already used elsewhere in the platform.
Let me know if you’d like help adjusting any of these. Really excited to see where this goes. Nice work overall!!!❤️🚀
Hey @piiq Thanks for looking through this! Agree on migrating to use FastMCP. Started looking into it after @deeleeramone mentioned it. More active and follows MCP standards more closely. It also supports Prompt templates and Resources which there might be some cool use-cases for. I started on a rewrite today, but have to figure out how to implement the "management" tools, aka how to hot-swap tools at runtime without restarting the server. Any ideas here would be very helpful. It is supposed to be possible based on the original documentation (https://modelcontextprotocol.io/docs/concepts/tools#python) I'll make sure to also move the configs and switch to |
- Also moves settings into the extention - Refactors the management tool - Add tool registry - Use Argpars
Okey so fully refactored to use FastMCP 2.0! Streamable HTTP is now supported in addition to SSE and stdio. Also future support for Resources and PrompTemplates when clients start supporting it. For multiple users it can be run with the discovery tools disabled, then the tools are set, so no dynamic changes. Then you can either limit the number of tools at startup or manually enable/disable e.g. in Cursor or Claud Desktop. There is also no longer a problem with shutting it down when clients are connected. And settings has been integrated in the extension and swapped to Next is to implement unit tests, and probably look into authentication |
I was able to access this data on Cursor. However, I would add some instructions on how to add it to a few of the popular MCP Clients, e.g. https://docs.financialdatasets.ai/mcp-server#cursor Also - I wasn't able to add it to the OpenBB workspace running locally due to CORS. I solved the issue by creating a proxy. I pushed the code here for context: #7156 But @MagnusS0 suggested having a look at https://gofastmcp.com/deployment/asgi instead |
@deeleeramone can the mcp potentially utilize the same cors settings that we set for the platform rest api from system settings json? |
Sorry, I guess it's already doing that. @DidierRLopes what do you have in the cors section of your system_settings.json? https://docs.openbb.co/platform/settings_and_environment_variables#api-settings |
I don't see why not, the default configuration is available via SystemService and just needs to be imported.
|
This is because the Middleware is not being added to the FastMCP server. |
I tested this and it works for SSE, but I get a sesssion ID error when using streamable-http. They mention this is due to some lifespan issue, but following their docs didn't fix it... My understanding was that it adopts the CORS settings from the underlying OpenBB platform FastAPI, but it seems that this is not the case and it needs to be set manually for the mcp server. So either add CORS settings to the mcp settings or use settings from the systems settings. |
The existing FastAPI app has a lifespan context, so perhaps related to "nested lifespans". Perhaps needs to be replaced explicitly?
|
Got it working by setting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MagnusS0 this is the most valuable and meaningful external contribution to the repo I've seen in a while.
Thank you for investing time into this. This is a very good addition to OpenBB
openbb_platform/extensions/mcp_server/openbb_mcp_server/tool_models.py
Outdated
Show resolved
Hide resolved
openbb_platform/extensions/mcp_server/openbb_mcp_server/utils/config.py
Outdated
Show resolved
Hide resolved
Thanks! Really appreciate that, and I'm happy to contribute 🫡 |
I second this! 💪 |
@MagnusS0 can you fix the linters please? 😂😅 Once we get all checks to pass this will be good to go |
Should pass now, ran the pre hooks 10x |
e00cf0f
Thank you very much for the contribution! |
Uploaded to PyPI https://pypi.org/project/openbb-mcp-server/ Should be able to start the server by running: uvx --from openbb-mcp-server --with openbb openbb-mcp |
@MagnusS0 since you own the pypi project we'll be tagging you for patches and uploads. If you want, you can add @deeleeramone or the openbb account to the project maintainers so that we can push updates. This is going to happen once in a while and most likely be related to patches of vulnerabilities in the package dependency tree. Let me know how you prefer to handle this |
@piiq Invited both opnebb and @deeleeramone as maintainers 🫡 Feel free to tag me anytime as well! |
MCP for OpenBB Platform
Edit (June 1, 2025): Based on feedback from maintainers, this is now implemented as a standalone extension following OpenBB's extension architecture.
Description
Summary of the change/feature:
This PR adds support for the Model Context Protocol (MCP) to the OpenBB Platform by mounting an MCP server to the REST FastAPI application. Using FastAPI-MCP, every REST endpoint is automatically exposed as an MCP tool, enabling seamless integration with LLM agents and tool-using frameworks. This creates a SSE endpoint and communicate over ASGI-transport layer by default.
Motivation and context:
With MCP, agents and LLMs can discover and invoke OpenBB API endpoints as tools, making it easy to build advanced workflows and automations. The extension provides configurable tool filtering by categories and providers, with helper endpoints for dynamic tool management:
get_available_tool_categories
,activate_tool_categories
,get_available_tools
, andactivate_tools
. This allows agents to dynamically control their toolset and avoid being overloaded with too many tools at once.Dependencies:
fastapi-mcp
(MIT License).How has this been tested?
Manual testing:
,
activate_tool_categories,
get_available_tools, and
activate_tools` endpoints for dynamic tool filtering.Known problem
Reproduction steps:
http://localhost:8001/mcp
using an MCP-compatible clientTest configuration:
If you modified/added command(s):
Checklist
feature/feature-name
orhotfix/hotfix-name
.