Skip to content

Commit 8840f95

Browse files
committed
remove base_url param entirely
1 parent 4d8ac55 commit 8840f95

File tree

5 files changed

+3
-75
lines changed

5 files changed

+3
-75
lines changed

README_zh-CN.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,14 @@ from fastapi_mcp import FastApiMCP
4747

4848
app = FastAPI()
4949

50-
mcp = FastApiMCP(
51-
app,
52-
53-
# 可选参数
54-
name="我的 API MCP",
55-
description="我的 API 描述",
56-
base_url="http://localhost:8000",
57-
)
50+
mcp = FastApiMCP(app)
5851

5952
# 直接将 MCP 服务器挂载到您的 FastAPI 应用
6053
mcp.mount()
6154
```
6255

6356
就是这样!您的自动生成的 MCP 服务器现在可以在 `https://app.base.url/mcp` 访问。
6457

65-
> **关于`base_url`的注意事项**:虽然`base_url`是可选的,但强烈建议您明确提供它。`base_url` 告诉 MCP 服务器在调用工具时向何处发送 API 请求。如果不提供,库将尝试自动确定 URL,这在部署环境中内部和外部 URL 不同时可能无法正确工作。
66-
6758
## 工具命名
6859

6960
FastAPI-MCP 使用 FastAPI 路由中的`operation_id`作为 MCP 工具的名称。如果您不指定`operation_id`,FastAPI 会自动生成一个,但这些名称可能比较晦涩。
@@ -101,7 +92,6 @@ app = FastAPI()
10192
mcp = FastApiMCP(
10293
app,
10394
name="我的 API MCP",
104-
base_url="http://localhost:8000",
10595
describe_all_responses=True, # 在工具描述中包含所有可能的响应模式
10696
describe_full_response_schema=True # 在工具描述中包含完整的 JSON 模式
10797
)
@@ -177,10 +167,7 @@ api_app = FastAPI()
177167
mcp_app = FastAPI()
178168

179169
# 从 API 应用创建 MCP 服务器
180-
mcp = FastApiMCP(
181-
api_app,
182-
base_url="http://api-host:8001", # API 应用将运行的 URL
183-
)
170+
mcp = FastApiMCP(api_app)
184171

185172
# 将 MCP 服务器挂载到单独的应用
186173
mcp.mount(mcp_app)

examples/mount_specific_router_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
app,
1818
name="Item API MCP",
1919
description="MCP server for the Item API",
20-
base_url="http://localhost:8000",
2120
)
2221

2322
# Mount the MCP server to a specific router.

fastapi_mcp/server.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ def __init__(
2323
fastapi: FastAPI,
2424
name: Optional[str] = None,
2525
description: Optional[str] = None,
26-
base_url: Annotated[
27-
Optional[str],
28-
Doc(
29-
"""
30-
Optionally specify an explicit base URL for the API server.
31-
This is only needed if FastApiMCP is deployed or mounted separately from the FastAPI app.
32-
"""
33-
),
34-
] = None,
3526
describe_all_responses: bool = False,
3627
describe_full_response_schema: bool = False,
3728
http_client: Annotated[
@@ -55,13 +46,8 @@ def __init__(
5546
fastapi: The FastAPI application
5647
name: Name for the MCP server (defaults to app.title)
5748
description: Description for the MCP server (defaults to app.description)
58-
base_url: Base URL for API requests. If not provided, the base URL will be determined from the
59-
FastAPI app's root path. Although optional, it is highly recommended to provide a base URL,
60-
as the root path would be different when the app is deployed.
6149
describe_all_responses: Whether to include all possible response schemas in tool descriptions
6250
describe_full_response_schema: Whether to include full json schema for responses in tool descriptions
63-
http_client: Optional HTTP client to use for API calls. If not provided, a new httpx.AsyncClient will be created.
64-
This is primarily for testing purposes.
6551
include_operations: List of operation IDs to include as MCP tools. Cannot be used with exclude_operations.
6652
exclude_operations: List of operation IDs to exclude from MCP tools. Cannot be used with include_operations.
6753
include_tags: List of tags to include as MCP tools. Cannot be used with exclude_tags.
@@ -82,7 +68,7 @@ def __init__(
8268
self.name = name or self.fastapi.title or "FastAPI MCP"
8369
self.description = description or self.fastapi.description
8470

85-
self._base_url = base_url or "http://apiserver"
71+
self._base_url = "http://apiserver"
8672
self._describe_all_responses = describe_all_responses
8773
self._describe_full_response_schema = describe_full_response_schema
8874
self._include_operations = include_operations
@@ -118,10 +104,6 @@ def setup_server(self) -> None:
118104
# Filter tools based on operation IDs and tags
119105
self.tools = self._filter_tools(all_tools, openapi_schema)
120106

121-
# Normalize base URL
122-
if self._base_url.endswith("/"):
123-
self._base_url = self._base_url[:-1]
124-
125107
# Create the MCP lowlevel server
126108
mcp_server: Server = Server(self.name, self.description)
127109

@@ -228,7 +210,6 @@ async def _execute_api_tool(
228210
Execute an MCP tool by making an HTTP request to the corresponding API endpoint.
229211
230212
Args:
231-
base_url: The base URL for the API
232213
tool_name: The name of the tool to execute
233214
arguments: The arguments for the tool
234215
operation_map: A mapping from tool names to operation details

tests/test_basic_functionality.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ def test_create_mcp_server(simple_fastapi_app: FastAPI):
1010
simple_fastapi_app,
1111
name="Test MCP Server",
1212
description="Test description",
13-
base_url="http://localhost:8000",
1413
)
1514

1615
# Verify the MCP server was created correctly
1716
assert mcp.name == "Test MCP Server"
1817
assert mcp.description == "Test description"
19-
assert mcp._base_url == "http://localhost:8000"
2018
assert isinstance(mcp.server, Server)
2119
assert len(mcp.tools) > 0, "Should have extracted tools from the app"
2220
assert len(mcp.operation_map) > 0, "Should have operation mapping"
@@ -35,9 +33,6 @@ def test_default_values(simple_fastapi_app: FastAPI):
3533
assert mcp.name == simple_fastapi_app.title
3634
assert mcp.description == simple_fastapi_app.description
3735

38-
# Default base URL should be derived or defaulted
39-
assert mcp._base_url is not None
40-
4136
# Mount with default path
4237
mcp.mount()
4338

tests/test_configuration.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ def test_default_configuration(simple_fastapi_app: FastAPI):
1313
assert mcp_server.name == simple_fastapi_app.title
1414
assert mcp_server.description == simple_fastapi_app.description
1515

16-
# Check default base URL
17-
assert mcp_server._base_url is not None
18-
assert mcp_server._base_url.startswith("http://")
19-
2016
# Check default options
2117
assert mcp_server._describe_all_responses is False
2218
assert mcp_server._describe_full_response_schema is False
@@ -27,13 +23,11 @@ def test_custom_configuration(simple_fastapi_app: FastAPI):
2723
# Create MCP server with custom options
2824
custom_name = "Custom MCP Server"
2925
custom_description = "A custom MCP server for testing"
30-
custom_base_url = "https://custom-api.example.com"
3126

3227
mcp_server = FastApiMCP(
3328
simple_fastapi_app,
3429
name=custom_name,
3530
description=custom_description,
36-
base_url=custom_base_url,
3731
describe_all_responses=True,
3832
describe_full_response_schema=True,
3933
)
@@ -42,41 +36,19 @@ def test_custom_configuration(simple_fastapi_app: FastAPI):
4236
assert mcp_server.name == custom_name
4337
assert mcp_server.description == custom_description
4438

45-
# Check custom base URL
46-
assert mcp_server._base_url == custom_base_url
47-
4839
# Check custom options
4940
assert mcp_server._describe_all_responses is True
5041
assert mcp_server._describe_full_response_schema is True
5142

5243

53-
def test_base_url_normalization(simple_fastapi_app: FastAPI):
54-
"""Test that base URLs are normalized correctly."""
55-
# Test with trailing slash
56-
mcp_server1 = FastApiMCP(
57-
simple_fastapi_app,
58-
base_url="http://example.com/api/",
59-
)
60-
assert mcp_server1._base_url == "http://example.com/api"
61-
62-
# Test without trailing slash
63-
mcp_server2 = FastApiMCP(
64-
simple_fastapi_app,
65-
base_url="http://example.com/api",
66-
)
67-
assert mcp_server2._base_url == "http://example.com/api"
68-
69-
7044
def test_describe_all_responses_config_simple_app(simple_fastapi_app: FastAPI):
7145
"""Test the describe_all_responses behavior with the simple app."""
7246
mcp_default = FastApiMCP(
7347
simple_fastapi_app,
74-
base_url="http://example.com",
7548
)
7649

7750
mcp_all_responses = FastApiMCP(
7851
simple_fastapi_app,
79-
base_url="http://example.com",
8052
describe_all_responses=True,
8153
)
8254

@@ -136,7 +108,6 @@ def test_describe_full_response_schema_config_simple_app(simple_fastapi_app: Fas
136108

137109
mcp_full_response_schema = FastApiMCP(
138110
simple_fastapi_app,
139-
base_url="http://example.com",
140111
describe_full_response_schema=True,
141112
)
142113

@@ -170,7 +141,6 @@ def test_describe_all_responses_and_full_response_schema_config_simple_app(simpl
170141

171142
mcp_all_responses_and_full_response_schema = FastApiMCP(
172143
simple_fastapi_app,
173-
base_url="http://example.com",
174144
describe_all_responses=True,
175145
describe_full_response_schema=True,
176146
)
@@ -208,12 +178,10 @@ def test_describe_all_responses_config_complex_app(complex_fastapi_app: FastAPI)
208178
"""Test the describe_all_responses behavior with the complex app."""
209179
mcp_default = FastApiMCP(
210180
complex_fastapi_app,
211-
base_url="http://example.com",
212181
)
213182

214183
mcp_all_responses = FastApiMCP(
215184
complex_fastapi_app,
216-
base_url="http://example.com",
217185
describe_all_responses=True,
218186
)
219187

@@ -281,7 +249,6 @@ def test_describe_full_response_schema_config_complex_app(complex_fastapi_app: F
281249
"""Test the describe_full_response_schema behavior with the complex app."""
282250
mcp_full_response_schema = FastApiMCP(
283251
complex_fastapi_app,
284-
base_url="http://example.com",
285252
describe_full_response_schema=True,
286253
)
287254

@@ -326,7 +293,6 @@ def test_describe_all_responses_and_full_response_schema_config_complex_app(comp
326293
"""Test the describe_all_responses and describe_full_response_schema together with the complex app."""
327294
mcp_all_responses_and_full_schema = FastApiMCP(
328295
complex_fastapi_app,
329-
base_url="http://example.com",
330296
describe_all_responses=True,
331297
describe_full_response_schema=True,
332298
)

0 commit comments

Comments
 (0)