Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
39b101f
1
Parent(s): 5a21dab
Update composition.mdx
Browse files- docs/servers/composition.mdx +0 -82
docs/servers/composition.mdx
CHANGED
|
@@ -206,85 +206,3 @@ main_mcp.mount(
|
|
| 206 |
<Warning>
|
| 207 |
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.
|
| 208 |
</Warning>
|
| 209 |
-
|
| 210 |
-
## Example: Modular Application
|
| 211 |
-
|
| 212 |
-
Here's a modular application structure using `import_server`:
|
| 213 |
-
|
| 214 |
-
<CodeGroup>
|
| 215 |
-
```python main.py
|
| 216 |
-
from fastmcp import FastMCP
|
| 217 |
-
import asyncio
|
| 218 |
-
from modules.text_server import text_mcp
|
| 219 |
-
from modules.data_server import data_mcp
|
| 220 |
-
import random
|
| 221 |
-
|
| 222 |
-
app = FastMCP(name="MainApplication")
|
| 223 |
-
|
| 224 |
-
async def setup():
|
| 225 |
-
# Import the utility servers
|
| 226 |
-
await app.import_server("text", text_mcp)
|
| 227 |
-
await app.import_server("data", data_mcp)
|
| 228 |
-
|
| 229 |
-
@app.tool()
|
| 230 |
-
def process_and_analyze(record_id: int) -> str:
|
| 231 |
-
"""Fetches a record and analyzes its string representation."""
|
| 232 |
-
# Get record data
|
| 233 |
-
record = {"id": record_id, "value": random.random()}
|
| 234 |
-
|
| 235 |
-
# Count words in the record string representation
|
| 236 |
-
word_count = len(str(record).split())
|
| 237 |
-
|
| 238 |
-
return (
|
| 239 |
-
f"Record {record_id} has {word_count} words in its string "
|
| 240 |
-
f"representation."
|
| 241 |
-
)
|
| 242 |
-
|
| 243 |
-
if __name__ == "__main__":
|
| 244 |
-
asyncio.run(setup())
|
| 245 |
-
app.run()
|
| 246 |
-
```
|
| 247 |
-
|
| 248 |
-
```python modules/text_server.py
|
| 249 |
-
from fastmcp import FastMCP
|
| 250 |
-
|
| 251 |
-
text_mcp = FastMCP(name="TextUtilities")
|
| 252 |
-
|
| 253 |
-
@text_mcp.tool()
|
| 254 |
-
def count_words(text: str) -> int:
|
| 255 |
-
"""Counts words in a text."""
|
| 256 |
-
return len(text.split())
|
| 257 |
-
|
| 258 |
-
@text_mcp.resource("resource://stopwords")
|
| 259 |
-
def get_stopwords() -> list[str]:
|
| 260 |
-
"""Return a list of common stopwords."""
|
| 261 |
-
return ["the", "a", "is", "in"]
|
| 262 |
-
```
|
| 263 |
-
|
| 264 |
-
```python modules/data_server.py
|
| 265 |
-
from fastmcp import FastMCP
|
| 266 |
-
import random
|
| 267 |
-
from typing import Dict
|
| 268 |
-
|
| 269 |
-
data_mcp = FastMCP(name="DataAPI")
|
| 270 |
-
|
| 271 |
-
@data_mcp.tool()
|
| 272 |
-
def fetch_record(record_id: int) -> Dict:
|
| 273 |
-
"""Fetches a dummy data record."""
|
| 274 |
-
return {"id": record_id, "value": random.random()}
|
| 275 |
-
|
| 276 |
-
@data_mcp.resource("data://schema/{table}")
|
| 277 |
-
def get_table_schema(table: str) -> Dict:
|
| 278 |
-
"""Provides a dummy schema for a table."""
|
| 279 |
-
return {"table": table, "columns": ["id", "value"]}
|
| 280 |
-
```
|
| 281 |
-
</CodeGroup>
|
| 282 |
-
|
| 283 |
-
Running `main.py` starts a server that exposes these prefixed components:
|
| 284 |
-
- `text_count_words`
|
| 285 |
-
- `data_fetch_record`
|
| 286 |
-
- `process_and_analyze` (defined in main app)
|
| 287 |
-
- `text+resource://stopwords`
|
| 288 |
-
- `data+data://schema/{table}` (template)
|
| 289 |
-
|
| 290 |
-
This pattern promotes code organization and reuse within your FastMCP projects.
|
|
|
|
| 206 |
<Warning>
|
| 207 |
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.
|
| 208 |
</Warning>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|