60e0ffc959
Upgrade checks / Notify on failure (push) Has been cancelled
Upgrade checks / Close issue on success (push) Has been cancelled
Schema Crash Test / Real-world schema crash test (232K schemas) (push) Has been cancelled
Run static analysis / static_analysis (push) Has been cancelled
Tests / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Tests / Tests with lowest-direct dependencies (push) Has been cancelled
Tests / MCP conformance tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Tests / Package install smoke (push) Has been cancelled
Upgrade checks / Static analysis (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Upgrade checks / Integration tests (push) Has been cancelled
Update MCPServerConfig Schema / update-config-schema (push) Has been cancelled
Update SDK Documentation / update-sdk-docs (push) Has been cancelled
64 lines
1.6 KiB
Plaintext
64 lines
1.6 KiB
Plaintext
---
|
|
title: Namespace Transform
|
|
sidebarTitle: Namespace
|
|
description: Prefix component names to prevent conflicts
|
|
icon: tag
|
|
---
|
|
|
|
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
|
|
<VersionBadge version="3.0.0" />
|
|
|
|
The `Namespace` transform prefixes all component names, preventing conflicts when composing multiple servers.
|
|
|
|
Tools and prompts receive an underscore-separated prefix. Resources and templates receive a path-segment prefix in their URIs.
|
|
|
|
| Component | Original | With `Namespace("api")` |
|
|
|-----------|----------|-------------------------|
|
|
| Tool | `my_tool` | `api_my_tool` |
|
|
| Prompt | `my_prompt` | `api_my_prompt` |
|
|
| Resource | `data://info` | `data://api/info` |
|
|
| Template | `data://{id}` | `data://api/{id}` |
|
|
|
|
The most common use is through the `mount()` method's `namespace` parameter.
|
|
|
|
```python
|
|
from fastmcp import FastMCP
|
|
|
|
weather = FastMCP("Weather")
|
|
calendar = FastMCP("Calendar")
|
|
|
|
@weather.tool
|
|
def get_data() -> str:
|
|
return "Weather data"
|
|
|
|
@calendar.tool
|
|
def get_data() -> str:
|
|
return "Calendar data"
|
|
|
|
# Without namespacing, these would conflict
|
|
main = FastMCP("Main")
|
|
main.mount(weather, namespace="weather")
|
|
main.mount(calendar, namespace="calendar")
|
|
|
|
# Clients see: weather_get_data, calendar_get_data
|
|
```
|
|
|
|
You can also apply namespacing directly using the `Namespace` transform.
|
|
|
|
```python
|
|
from fastmcp import FastMCP
|
|
from fastmcp.server.transforms import Namespace
|
|
|
|
mcp = FastMCP("Server")
|
|
|
|
@mcp.tool
|
|
def greet(name: str) -> str:
|
|
return f"Hello, {name}!"
|
|
|
|
# Namespace all components
|
|
mcp.add_transform(Namespace("api"))
|
|
|
|
# Tool is now: api_greet
|
|
```
|