fastmcp/docs/servers/transforms/namespace.mdx
nate nowack a4fa3d2e31
Namespace resource links returned by tools (#6720)
Co-authored-by: DanielSong <102885173+Liu8Can@users.noreply.github.com>
Co-authored-by: ldlda <118123956+ldlda@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QCgr8md9CNeZCywsn9adrG
2026-11-23 08:35:22 +00:00

65 lines
2.3 KiB
Text

---
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 with `scheme://` URIs receive a path-segment prefix. URIs without `://`, such as `urn:example:config`, remain unchanged for both listing and reading. They can collide across namespaces; equal identifiers follow the normal [provider resolution order](/servers/composition#conflict-resolution).
| 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}` |
Resource links that a namespaced tool returns get the same URI prefix, so a client can read a returned link through the server that returned it. A tool on a server mounted with `namespace="api"` that returns a `ResourceLink` to `data://info` reaches the client as a link to `data://api/info`. Every `scheme://` link is rewritten this way, including URLs the server doesn't serve, so a namespaced tool's link to `https://example.com/x` becomes `https://api/example.com/x`. Return an external URL as text if it must reach the client unchanged.
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
```