Spaces:
Running
Running
Merge pull request #800 from jlowin/settings-dep
Browse files- docs/servers/fastmcp.mdx +58 -15
- src/fastmcp/server/server.py +9 -0
docs/servers/fastmcp.mdx
CHANGED
|
@@ -214,33 +214,76 @@ proxy = FastMCP.as_proxy(backend, name="ProxyServer")
|
|
| 214 |
|
| 215 |
## Server Configuration
|
| 216 |
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
```python
|
| 220 |
from fastmcp import FastMCP
|
| 221 |
|
| 222 |
-
# Configure
|
| 223 |
mcp = FastMCP(
|
| 224 |
name="ConfiguredServer",
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
```
|
| 233 |
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
-
|
| 237 |
-
- **`port`**: Port number for SSE transport (default: 8000)
|
| 238 |
-
- **`log_level`**: Logging level (default: "INFO")
|
| 239 |
-
- **`on_duplicate_tools`**: How to handle duplicate tool registrations
|
| 240 |
-
- **`on_duplicate_resources`**: How to handle duplicate resource registrations
|
| 241 |
-
- **`on_duplicate_prompts`**: How to handle duplicate prompt registrations
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
### Custom Tool Serialization
|
| 246 |
|
|
|
|
| 214 |
|
| 215 |
## Server Configuration
|
| 216 |
|
| 217 |
+
Servers can be configured using a combination of initialization arguments, global settings, and transport-specific settings.
|
| 218 |
+
|
| 219 |
+
### Server-Specific Configuration
|
| 220 |
+
|
| 221 |
+
Server-specific settings are passed when creating the `FastMCP` instance and control server behavior:
|
| 222 |
|
| 223 |
```python
|
| 224 |
from fastmcp import FastMCP
|
| 225 |
|
| 226 |
+
# Configure server-specific settings
|
| 227 |
mcp = FastMCP(
|
| 228 |
name="ConfiguredServer",
|
| 229 |
+
dependencies=["requests", "pandas>=2.0.0"], # Optional server dependencies
|
| 230 |
+
include_tags={"public", "api"}, # Only expose these tagged components
|
| 231 |
+
exclude_tags={"internal", "deprecated"}, # Hide these tagged components
|
| 232 |
+
on_duplicate_tools="error", # Handle duplicate registrations
|
| 233 |
+
on_duplicate_resources="warn",
|
| 234 |
+
on_duplicate_prompts="replace",
|
| 235 |
)
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
### Global Settings
|
| 239 |
+
|
| 240 |
+
Global settings affect all FastMCP servers and can be configured via environment variables (prefixed with `FASTMCP_`) or in a `.env` file:
|
| 241 |
|
| 242 |
+
```python
|
| 243 |
+
import fastmcp
|
| 244 |
+
|
| 245 |
+
# Access global settings
|
| 246 |
+
print(fastmcp.settings.log_level) # Default: "INFO"
|
| 247 |
+
print(fastmcp.settings.mask_error_details) # Default: False
|
| 248 |
+
print(fastmcp.settings.resource_prefix_format) # Default: "path"
|
| 249 |
```
|
| 250 |
|
| 251 |
+
Common global settings include:
|
| 252 |
+
- **`log_level`**: Logging level ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"), set with `FASTMCP_LOG_LEVEL`
|
| 253 |
+
- **`mask_error_details`**: Whether to hide detailed error information from clients, set with `FASTMCP_MASK_ERROR_DETAILS`
|
| 254 |
+
- **`resource_prefix_format`**: How to format resource prefixes ("path" or "protocol"), set with `FASTMCP_RESOURCE_PREFIX_FORMAT`
|
| 255 |
|
| 256 |
+
### Transport-Specific Configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
Transport settings are provided when running the server and control network behavior:
|
| 259 |
+
|
| 260 |
+
```python
|
| 261 |
+
# Configure transport when running
|
| 262 |
+
mcp.run(
|
| 263 |
+
transport="streamable-http",
|
| 264 |
+
host="0.0.0.0", # Bind to all interfaces
|
| 265 |
+
port=9000, # Custom port
|
| 266 |
+
log_level="DEBUG", # Override global log level
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
# Or for async usage
|
| 270 |
+
await mcp.run_async(
|
| 271 |
+
transport="streamable-http",
|
| 272 |
+
host="127.0.0.1",
|
| 273 |
+
port=8080,
|
| 274 |
+
)
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
### Environment Variables
|
| 278 |
+
|
| 279 |
+
Settings can be configured via environment variables:
|
| 280 |
+
|
| 281 |
+
```bash
|
| 282 |
+
# Global settings
|
| 283 |
+
export FASTMCP_LOG_LEVEL=DEBUG
|
| 284 |
+
export FASTMCP_MASK_ERROR_DETAILS=True
|
| 285 |
+
export FASTMCP_RESOURCE_PREFIX_FORMAT=protocol
|
| 286 |
+
```
|
| 287 |
|
| 288 |
### Custom Tool Serialization
|
| 289 |
|
src/fastmcp/server/server.py
CHANGED
|
@@ -252,6 +252,15 @@ class FastMCP(Generic[LifespanResultT]):
|
|
| 252 |
combined_settings = fastmcp.settings.model_dump() | deprecated_settings
|
| 253 |
self._deprecated_settings = Settings(**combined_settings)
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
@property
|
| 256 |
def name(self) -> str:
|
| 257 |
return self._mcp_server.name
|
|
|
|
| 252 |
combined_settings = fastmcp.settings.model_dump() | deprecated_settings
|
| 253 |
self._deprecated_settings = Settings(**combined_settings)
|
| 254 |
|
| 255 |
+
@property
|
| 256 |
+
def settings(self) -> Settings:
|
| 257 |
+
warnings.warn(
|
| 258 |
+
"Accessing `.settings` on a FastMCP instance is deprecated. Use the global `fastmcp.settings` instead.",
|
| 259 |
+
DeprecationWarning,
|
| 260 |
+
stacklevel=2,
|
| 261 |
+
)
|
| 262 |
+
return self._deprecated_settings
|
| 263 |
+
|
| 264 |
@property
|
| 265 |
def name(self) -> str:
|
| 266 |
return self._mcp_server.name
|