FastMCP (Python)
This documentation explains how to protect your FastMCP application thanks to the usage of the Python ASGI module.
Prerequisites
- The server-side key available in your DataDome dashboard
- The FastMCP server must be accessible over HTTP.
- The FastMCP version must be higher or equal than
2.3.2to use custom middleware.
Usage
Add the Python ASGI middleware as a custom middleware for your MCP application.
from fastmcp import FastMCP
from starlette.middleware import Middleware
from datadome_asgi import DataDomeMiddleware
# Create FastMCP application
mcp = FastMCP()
# Define the list of Middleware
middleware = [
Middleware(
DataDomeMiddleware,
server_side_key="YOUR_SERVER_SIDE_KEY",
)
]
# Use MCP server over HTTP
app = mcp.http_app(middleware=middleware)Your MCP server is now protected with DataDome!
Settings
| Setting | Description | Default value |
|---|---|---|
server_side_key | Your DataDome server-side key. | |
app | The ASGI application the middleware needs to be attached to. | |
url_pattern_exclusion | Regex to match to exclude requests from being processed with the Protection API. | r"\.(avi|avif|bmp|css|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|json|less|map|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|otf|png|svg|svgz|swf|tiff|ttf|wav|webm|webp|woff|woff2|xml|zip)$" |
url_pattern_inclusion | Regex to match to process the request with the Protection API. | "" |
endpoint_host | Host of the Protection API. | api.datadome.co |
timeout | The timeout limit for requests sent to the Protection API, in milliseconds. | 150 |
enable_mcp_support | Boolean to enable Model Context Protocol support. | false |
mcp_endpoint | MCP endpoint on which the information should be extracted. You can specify several endpoints separated with commas. | mcp |
Here is an example of configuration with default values that you can modify according to your needs:
from fastmcp import FastMCP
from starlette.middleware import Middleware
from datadome_asgi import DataDomeMiddleware
# Create FastMCP application
mcp = FastMCP()
# Define the list of Middleware
middleware = [
Middleware(
DataDomeMiddleware,
server_side_key="YOUR_SERVER_SIDE_KEY",
urlPatternExclusion=r"\.(avi|avif|bmp|css|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|json|less|map|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|otf|png|svg|svgz|swf|tiff|ttf|wav|webm|webp|woff|woff2|xml|zip)$",
urlPatternInclusion="",
endpoint_host="api.datadome.co",
timeout=150,
enable_mcp_support=False,
mcp_endpoint="/mcp"
)
]
# Use MCP server over HTTP
app = mcp.http_app(middleware=middleware)Advanced configuration
Enable MCP support
You can enable support for the Model Context Protocol and extract information from the request body.
Set the enable_mcp_support setting to true.
Once enabled, requests targeting an MCP endpoint will be analyzed to extract MCP properties according to the official specification:
mcp-session-idandmcp-protocol-versionheadersjsonRpcVersion,jsonRpcRequestId, andmcpMethodrequest body fieldsmcpParamsClientInfoNameandmcpParamsClientInfoVersionrequest body fields when the method isinitializemcpParamsToolNamerequest body field when the method istools/call
Starting from version 1.2.0, you can customize the MCP endpoint by defining the mcp_endpoint setting.
Updated 5 days ago
Did this page help you?

