Update composition.mdx

This commit is contained in:
Jeremiah Lowin 2025-05-10 15:52:10 -04:00
commit 2416561b5c

View file

@ -206,85 +206,3 @@ main_mcp.mount(
<Warning>
Be cautious when choosing separators. Some MCP clients (like Claude Desktop) might have restrictions on characters allowed in tool names (e.g., `/` might not be supported). The defaults (`_` for names, `+` for URIs) are generally safe.
</Warning>
## Example: Modular Application
Here's a modular application structure using `import_server`:
<CodeGroup>
```python main.py
from fastmcp import FastMCP
import asyncio
from modules.text_server import text_mcp
from modules.data_server import data_mcp
import random
app = FastMCP(name="MainApplication")
async def setup():
# Import the utility servers
await app.import_server("text", text_mcp)
await app.import_server("data", data_mcp)
@app.tool()
def process_and_analyze(record_id: int) -> str:
"""Fetches a record and analyzes its string representation."""
# Get record data
record = {"id": record_id, "value": random.random()}
# Count words in the record string representation
word_count = len(str(record).split())
return (
f"Record {record_id} has {word_count} words in its string "
f"representation."
)
if __name__ == "__main__":
asyncio.run(setup())
app.run()
```
```python modules/text_server.py
from fastmcp import FastMCP
text_mcp = FastMCP(name="TextUtilities")
@text_mcp.tool()
def count_words(text: str) -> int:
"""Counts words in a text."""
return len(text.split())
@text_mcp.resource("resource://stopwords")
def get_stopwords() -> list[str]:
"""Return a list of common stopwords."""
return ["the", "a", "is", "in"]
```
```python modules/data_server.py
from fastmcp import FastMCP
import random
from typing import Dict
data_mcp = FastMCP(name="DataAPI")
@data_mcp.tool()
def fetch_record(record_id: int) -> Dict:
"""Fetches a dummy data record."""
return {"id": record_id, "value": random.random()}
@data_mcp.resource("data://schema/{table}")
def get_table_schema(table: str) -> Dict:
"""Provides a dummy schema for a table."""
return {"table": table, "columns": ["id", "value"]}
```
</CodeGroup>
Running `main.py` starts a server that exposes these prefixed components:
- `text_count_words`
- `data_fetch_record`
- `process_and_analyze` (defined in main app)
- `text+resource://stopwords`
- `data+data://schema/{table}` (template)
This pattern promotes code organization and reuse within your FastMCP projects.