Skip to content

Expose FastMCP object to allow for use of FastMCP Features#49

Open
pazzelli wants to merge 5 commits intoMariaDB:mainfrom
pazzelli:expose-fastmcp-object
Open

Expose FastMCP object to allow for use of FastMCP Features#49
pazzelli wants to merge 5 commits intoMariaDB:mainfrom
pazzelli:expose-fastmcp-object

Conversation

@pazzelli
Copy link
Copy Markdown

@pazzelli pazzelli commented Mar 28, 2026

Purpose

The purpose of this change is to expose the FastMCP object contained inside server.py to the outside world by refactoring the startup logic in the if __name__ == "__main__" block.

This allows use of public features on the FastMCP object (e.g. Resources) by exposing the server.mcp object using code such as:

from server import get_arg_parser, MariaDBServer                                                                                                                            

server = MariaDBServer()

@server.mcp.resource("schema://context")                                                                                                                                    
  def schema_context() -> str:
      return Path("my_schema_context.md").read_text()                                                                                                                          
   
args = get_arg_parser().parse_args()                                                                                                                                          
server.start(args.transport, args.host, args.port, args.path)

The example above would allow providing a consumer with additional text-based context on the underlying MariaDB schema, which could help an LLM to make better decisions about which data to use at query time.

Change Summary

  • Refactored server.py's startup logic out of the bare if __name__ == "__main__" block into two new public members:
    • MariaDBServer.start(transport, host, port, path) — a new, synchronous entry point that wraps run_async_server()
    • get_arg_parser() -> ArgumentParser — returns the CLI argument parser so callers can add their own custom args (if necessary) before parsing and passing to start()
  • Added src/tests/test_custom_resource.py — instantiates MariaDBServer, registers a custom schema://context resource on the exposed mcp object, then uses a FastMCP Client to verify the resource appears in the resource list and that its content can be read correctly

This was previously impossible since the entire server startup lifecycle was buried inside if __name__ == "__main__"

Tests

  • test_custom_resource_is_listedschema://context appears in list_resources()
  • test_custom_resource_contentread_resource("schema://context") returns the expected string

Test Results (local)

(mariadb-server) E:\johntest\mariadb-mcp\src\tests>python -m unittest test_custom_resource.py

2026-03-28 15:07:25,324 - config - INFO - Selected Embedding Provider: None
2026-03-28 15:07:25,324 - config - INFO - No EMBEDDING_PROVIDER selected or it is set to None. Disabling embedding features.
2026-03-28 15:07:25,324 - config - INFO - Read-only mode: True
2026-03-28 15:07:25,324 - config - INFO - Logging to console and to file: logs/mcp_server.log (Level: INFO, MaxSize: 10485760B, Backups: 5)
2026-03-28 15:07:27,908 - config - WARNING - Google Generative AI SDK ('google-genai' package) not installed. Gemini provider will not be available. Error: No module named 'google.api_core'

2026-03-28 15:07:27,910 - config - INFO - Current sys.path: ['E:\\johntest\\mariadb-mcp\\src', 'E:\\ServerFolders\\Company\\IT Dept\\johntest\\mariadb-mcp\\src\\tests', ...]
2026-03-28 15:07:27,941 - config - INFO - Initializing MariaDB_Server...
2026-03-28 15:07:27,943 - config - WARNING - Server running in READ-ONLY mode. Write operations are disabled.
2026-03-28 15:07:28,022 - mcp.server.lowlevel.server - INFO - Processing request of type ReadResourceRequest
.2026-03-28 15:07:28,043 - config - INFO - Initializing MariaDB_Server...
2026-03-28 15:07:28,043 - config - WARNING - Server running in READ-ONLY mode. Write operations are disabled.
2026-03-28 15:07:28,074 - mcp.server.lowlevel.server - INFO - Processing request of type ListResourcesRequest
.
----------------------------------------------------------------------
Ran 2 tests in 0.193s

OK

In MCP Inspector:

Screenshot 2026-03-28 at 3 20 50 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant