or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

authentication.mdindex.mdmcp-server.md

mcp-server.mddocs/

0

# MCP Server Creation

1

2

Core functionality for creating and configuring MCP servers from FastAPI applications. The `FastApiMCP` class automatically converts your FastAPI endpoints into MCP tools that AI agents can discover and invoke.

3

4

## Capabilities

5

6

### Server Initialization

7

8

Creates an MCP server instance from a FastAPI application with comprehensive configuration options for tool generation, filtering, and response handling.

9

10

```python { .api }

11

class FastApiMCP:

12

def __init__(

13

self,

14

fastapi: FastAPI,

15

name: Optional[str] = None,

16

description: Optional[str] = None,

17

describe_all_responses: bool = False,

18

describe_full_response_schema: bool = False,

19

http_client: Optional[httpx.AsyncClient] = None,

20

include_operations: Optional[List[str]] = None,

21

exclude_operations: Optional[List[str]] = None,

22

include_tags: Optional[List[str]] = None,

23

exclude_tags: Optional[List[str]] = None,

24

auth_config: Optional[AuthConfig] = None,

25

headers: List[str] = ["authorization"]

26

):

27

"""

28

Create an MCP server from a FastAPI application.

29

30

Parameters:

31

- fastapi: The FastAPI application to create an MCP server from

32

- name: Name for the MCP server (defaults to app.title)

33

- description: Description for the MCP server (defaults to app.description)

34

- describe_all_responses: Whether to include all possible response schemas in tool descriptions

35

- describe_full_response_schema: Whether to include full JSON schema for responses

36

- http_client: Optional custom httpx.AsyncClient for API calls

37

- include_operations: List of operation IDs to include as MCP tools (mutually exclusive with exclude_operations)

38

- exclude_operations: List of operation IDs to exclude from MCP tools (mutually exclusive with include_operations)

39

- include_tags: List of tags to include as MCP tools (mutually exclusive with exclude_tags)

40

- exclude_tags: List of tags to exclude from MCP tools (mutually exclusive with include_tags)

41

- auth_config: Configuration for MCP authentication

42

- headers: HTTP header names to forward from MCP requests (defaults to ['authorization'])

43

"""

44

```

45

46

#### Example Usage

47

48

```python

49

from fastapi import FastAPI, HTTPException, Depends

50

from fastapi_mcp import FastApiMCP, AuthConfig

51

52

app = FastAPI(title="User Management API")

53

54

# Create MCP server with operation filtering

55

mcp_server = FastApiMCP(

56

fastapi=app,

57

name="User Management MCP",

58

include_tags=["users", "auth"], # Only include endpoints tagged with 'users' or 'auth'

59

describe_all_responses=True, # Include all response schemas

60

headers=["authorization", "x-api-key"] # Forward these headers

61

)

62

```

63

64

### HTTP Transport Mounting

65

66

Mounts the MCP server with HTTP transport, which is the recommended transport method for production use.

67

68

```python { .api }

69

def mount_http(

70

self,

71

router: Optional[FastAPI | APIRouter] = None,

72

mount_path: str = "/mcp"

73

) -> None:

74

"""

75

Mount the MCP server with HTTP transport to any FastAPI app or APIRouter.

76

77

Parameters:

78

- router: The FastAPI app or APIRouter to mount to (defaults to the original FastAPI app)

79

- mount_path: Path where the MCP server will be mounted (defaults to '/mcp')

80

"""

81

```

82

83

#### Example Usage

84

85

```python

86

from fastapi import APIRouter

87

88

# Mount to the same app

89

mcp_server.mount_http()

90

91

# Mount to a different app or router

92

api_router = APIRouter(prefix="/api/v1")

93

mcp_server.mount_http(router=api_router, mount_path="/mcp-tools")

94

95

# The MCP server will be available at the mount path

96

# AI clients can connect via HTTP to interact with your API

97

```

98

99

### SSE Transport Mounting

100

101

Mounts the MCP server with Server-Sent Events (SSE) transport for streaming communication.

102

103

```python { .api }

104

def mount_sse(

105

self,

106

router: Optional[FastAPI | APIRouter] = None,

107

mount_path: str = "/sse"

108

) -> None:

109

"""

110

Mount the MCP server with SSE transport to any FastAPI app or APIRouter.

111

112

Parameters:

113

- router: The FastAPI app or APIRouter to mount to (defaults to the original FastAPI app)

114

- mount_path: Path where the MCP server will be mounted (defaults to '/sse')

115

"""

116

```

117

118

#### Example Usage

119

120

```python

121

# Mount SSE transport

122

mcp_server.mount_sse(mount_path="/mcp-sse")

123

124

# Creates endpoints:

125

# GET /mcp-sse - SSE connection endpoint

126

# POST /mcp-sse/messages/ - Message posting endpoint

127

```

128

129

### Legacy Mount Method (Deprecated)

130

131

```python { .api }

132

def mount(

133

self,

134

router: Optional[FastAPI | APIRouter] = None,

135

mount_path: str = "/mcp",

136

transport: Literal["sse"] = "sse"

137

) -> None:

138

"""

139

[DEPRECATED] Mount the MCP server to any FastAPI app or APIRouter.

140

141

Use mount_http() for HTTP transport or mount_sse() for SSE transport instead.

142

"""

143

```

144

145

### Server Properties

146

147

Access to the underlying MCP server components and generated tools.

148

149

```python { .api }

150

@property

151

def server(self) -> Server:

152

"""The underlying MCP server instance."""

153

154

@property

155

def tools(self) -> List[types.Tool]:

156

"""List of generated MCP tools from FastAPI endpoints."""

157

158

@property

159

def operation_map(self) -> Dict[str, Dict[str, Any]]:

160

"""Mapping from tool names to operation details."""

161

162

@property

163

def fastapi(self) -> FastAPI:

164

"""The FastAPI application instance."""

165

166

@property

167

def name(self) -> str:

168

"""MCP server name."""

169

170

@property

171

def description(self) -> str:

172

"""MCP server description."""

173

```

174

175

## Advanced Configuration

176

177

### Operation Filtering

178

179

Control which FastAPI endpoints become MCP tools using operation IDs or tags:

180

181

```python

182

# Include only specific operations

183

mcp_server = FastApiMCP(

184

fastapi=app,

185

include_operations=["get_user", "create_user", "update_user"]

186

)

187

188

# Exclude specific operations

189

mcp_server = FastApiMCP(

190

fastapi=app,

191

exclude_operations=["delete_user", "admin_only_endpoint"]

192

)

193

194

# Include by tags

195

mcp_server = FastApiMCP(

196

fastapi=app,

197

include_tags=["public", "user-management"]

198

)

199

200

# Exclude by tags

201

mcp_server = FastApiMCP(

202

fastapi=app,

203

exclude_tags=["internal", "admin"]

204

)

205

```

206

207

### Custom HTTP Client

208

209

Provide a custom HTTP client for API calls with specific configuration:

210

211

```python

212

import httpx

213

214

# Custom client with authentication

215

custom_client = httpx.AsyncClient(

216

headers={"Authorization": "Bearer token"},

217

timeout=30.0

218

)

219

220

mcp_server = FastApiMCP(

221

fastapi=app,

222

http_client=custom_client

223

)

224

```

225

226

### Response Schema Configuration

227

228

Control how response schemas are included in tool descriptions:

229

230

```python

231

mcp_server = FastApiMCP(

232

fastapi=app,

233

describe_all_responses=True, # Include all possible HTTP response codes

234

describe_full_response_schema=True # Include complete JSON schema for responses

235

)

236

```

237

238

### Header Forwarding

239

240

Specify which HTTP headers to forward from MCP requests to API calls:

241

242

```python

243

mcp_server = FastApiMCP(

244

fastapi=app,

245

headers=["authorization", "x-api-key", "x-correlation-id"]

246

)

247

```