|
| 1 | +from fastapi import FastAPI |
| 2 | + |
| 3 | +from fastapi_mcp import FastApiMCP |
| 4 | + |
| 5 | + |
| 6 | +def test_default_configuration(simple_fastapi_app: FastAPI): |
| 7 | + """Test the default configuration of FastApiMCP.""" |
| 8 | + # Create MCP server with defaults |
| 9 | + mcp_server = FastApiMCP(simple_fastapi_app) |
| 10 | + |
| 11 | + # Check default name and description |
| 12 | + assert mcp_server.name == simple_fastapi_app.title |
| 13 | + assert mcp_server.description == simple_fastapi_app.description |
| 14 | + |
| 15 | + # Check default base URL |
| 16 | + assert mcp_server._base_url is not None |
| 17 | + assert mcp_server._base_url.startswith("http://") |
| 18 | + |
| 19 | + # Check default options |
| 20 | + assert mcp_server._describe_all_responses is False |
| 21 | + assert mcp_server._describe_full_response_schema is False |
| 22 | + |
| 23 | + |
| 24 | +def test_custom_configuration(simple_fastapi_app: FastAPI): |
| 25 | + """Test a custom configuration of FastApiMCP.""" |
| 26 | + # Create MCP server with custom options |
| 27 | + custom_name = "Custom MCP Server" |
| 28 | + custom_description = "A custom MCP server for testing" |
| 29 | + custom_base_url = "https://custom-api.example.com" |
| 30 | + |
| 31 | + mcp_server = FastApiMCP( |
| 32 | + simple_fastapi_app, |
| 33 | + name=custom_name, |
| 34 | + description=custom_description, |
| 35 | + base_url=custom_base_url, |
| 36 | + describe_all_responses=True, |
| 37 | + describe_full_response_schema=True, |
| 38 | + ) |
| 39 | + |
| 40 | + # Check custom name and description |
| 41 | + assert mcp_server.name == custom_name |
| 42 | + assert mcp_server.description == custom_description |
| 43 | + |
| 44 | + # Check custom base URL |
| 45 | + assert mcp_server._base_url == custom_base_url |
| 46 | + |
| 47 | + # Check custom options |
| 48 | + assert mcp_server._describe_all_responses is True |
| 49 | + assert mcp_server._describe_full_response_schema is True |
| 50 | + |
| 51 | + |
| 52 | +def test_base_url_normalization(simple_fastapi_app: FastAPI): |
| 53 | + """Test that base URLs are normalized correctly.""" |
| 54 | + # Test with trailing slash |
| 55 | + mcp_server1 = FastApiMCP( |
| 56 | + simple_fastapi_app, |
| 57 | + base_url="http://example.com/api/", |
| 58 | + ) |
| 59 | + assert mcp_server1._base_url == "http://example.com/api" |
| 60 | + |
| 61 | + # Test without trailing slash |
| 62 | + mcp_server2 = FastApiMCP( |
| 63 | + simple_fastapi_app, |
| 64 | + base_url="http://example.com/api", |
| 65 | + ) |
| 66 | + assert mcp_server2._base_url == "http://example.com/api" |
| 67 | + |
| 68 | + |
| 69 | +def test_describe_all_responses_config_simple_app(simple_fastapi_app: FastAPI): |
| 70 | + """Test the describe_all_responses behavior with the simple app.""" |
| 71 | + mcp_default = FastApiMCP( |
| 72 | + simple_fastapi_app, |
| 73 | + base_url="http://example.com", |
| 74 | + ) |
| 75 | + |
| 76 | + mcp_all_responses = FastApiMCP( |
| 77 | + simple_fastapi_app, |
| 78 | + base_url="http://example.com", |
| 79 | + describe_all_responses=True, |
| 80 | + ) |
| 81 | + |
| 82 | + for tool in mcp_default.tools: |
| 83 | + assert tool.description is not None |
| 84 | + if tool.name != "delete_item": |
| 85 | + assert tool.description.count("**200**") == 1, "The description should contain a 200 status code" |
| 86 | + assert tool.description.count("**Example Response:**") == 1, ( |
| 87 | + "The description should only contain one example response" |
| 88 | + ) |
| 89 | + assert tool.description.count("**Output Schema:**") == 0, ( |
| 90 | + "The description should not contain a full output schema" |
| 91 | + ) |
| 92 | + else: |
| 93 | + # The delete endpoint in the Items API returns a 204 status code |
| 94 | + assert tool.description.count("**200**") == 0, "The description should not contain a 200 status code" |
| 95 | + assert tool.description.count("**204**") == 1, "The description should contain a 204 status code" |
| 96 | + # The delete endpoint in the Items API returns a 204 status code and has no response body |
| 97 | + assert tool.description.count("**Example Response:**") == 0, ( |
| 98 | + "The description should not contain any example responses" |
| 99 | + ) |
| 100 | + assert tool.description.count("**Output Schema:**") == 0, ( |
| 101 | + "The description should not contain a full output schema" |
| 102 | + ) |
| 103 | + |
| 104 | + for tool in mcp_all_responses.tools: |
| 105 | + assert tool.description is not None |
| 106 | + if tool.name != "delete_item": |
| 107 | + assert tool.description.count("**200**") == 1, "The description should contain a 200 status code" |
| 108 | + assert tool.description.count("**422**") == 1, "The description should contain a 422 status code" |
| 109 | + assert tool.description.count("**Example Response:**") == 2, ( |
| 110 | + "The description should contain two example responses" |
| 111 | + ) |
| 112 | + assert tool.description.count("**Output Schema:**") == 0, ( |
| 113 | + "The description should not contain a full output schema" |
| 114 | + ) |
| 115 | + else: |
| 116 | + assert tool.description.count("**204**") == 1, "The description should contain a 204 status code" |
| 117 | + assert tool.description.count("**422**") == 1, "The description should contain a 422 status code" |
| 118 | + # The delete endpoint in the Items API returns a 204 status code and has no response body |
| 119 | + # But FastAPI's default 422 response should be present |
| 120 | + # So just 1 instance of Example Response should be present |
| 121 | + assert tool.description.count("**Example Response:**") == 1, ( |
| 122 | + "The description should contain one example response" |
| 123 | + ) |
| 124 | + assert tool.description.count("**Output Schema:**") == 0, ( |
| 125 | + "The description should not contain any output schema" |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def test_describe_full_response_schema_config_simple_app(simple_fastapi_app: FastAPI): |
| 130 | + """Test the describe_full_response_schema behavior with the simple app.""" |
| 131 | + |
| 132 | + mcp_full_response_schema = FastApiMCP( |
| 133 | + simple_fastapi_app, |
| 134 | + base_url="http://example.com", |
| 135 | + describe_full_response_schema=True, |
| 136 | + ) |
| 137 | + |
| 138 | + for tool in mcp_full_response_schema.tools: |
| 139 | + assert tool.description is not None |
| 140 | + if tool.name != "delete_item": |
| 141 | + assert tool.description.count("**200**") == 1, "The description should contain a 200 status code" |
| 142 | + assert tool.description.count("**Example Response:**") == 1, ( |
| 143 | + "The description should only contain one example response" |
| 144 | + ) |
| 145 | + assert tool.description.count("**Output Schema:**") == 1, ( |
| 146 | + "The description should contain one full output schema" |
| 147 | + ) |
| 148 | + else: |
| 149 | + # The delete endpoint in the Items API returns a 204 status code |
| 150 | + assert tool.description.count("**200**") == 0, "The description should not contain a 200 status code" |
| 151 | + assert tool.description.count("**204**") == 1, "The description should contain a 204 status code" |
| 152 | + # The delete endpoint in the Items API returns a 204 status code and has no response body |
| 153 | + assert tool.description.count("**Example Response:**") == 0, ( |
| 154 | + "The description should not contain any example responses" |
| 155 | + ) |
| 156 | + assert tool.description.count("**Output Schema:**") == 0, ( |
| 157 | + "The description should not contain a full output schema" |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +def test_describe_all_responses_and_full_response_schema_config_simple_app(simple_fastapi_app: FastAPI): |
| 162 | + """Test the describe_all_responses and describe_full_response_schema params together with the simple app.""" |
| 163 | + |
| 164 | + mcp_all_responses_and_full_response_schema = FastApiMCP( |
| 165 | + simple_fastapi_app, |
| 166 | + base_url="http://example.com", |
| 167 | + describe_all_responses=True, |
| 168 | + describe_full_response_schema=True, |
| 169 | + ) |
| 170 | + |
| 171 | + for tool in mcp_all_responses_and_full_response_schema.tools: |
| 172 | + assert tool.description is not None |
| 173 | + if tool.name != "delete_item": |
| 174 | + assert tool.description.count("**200**") == 1, "The description should contain a 200 status code" |
| 175 | + assert tool.description.count("**422**") == 1, "The description should contain a 422 status code" |
| 176 | + assert tool.description.count("**Example Response:**") == 2, ( |
| 177 | + "The description should contain two example responses" |
| 178 | + ) |
| 179 | + assert tool.description.count("**Output Schema:**") == 2, ( |
| 180 | + "The description should contain two full output schemas" |
| 181 | + ) |
| 182 | + else: |
| 183 | + # The delete endpoint in the Items API returns a 204 status code |
| 184 | + assert tool.description.count("**200**") == 0, "The description should not contain a 200 status code" |
| 185 | + assert tool.description.count("**204**") == 1, "The description should contain a 204 status code" |
| 186 | + assert tool.description.count("**422**") == 1, "The description should contain a 422 status code" |
| 187 | + # The delete endpoint in the Items API returns a 204 status code and has no response body |
| 188 | + # But FastAPI's default 422 response should be present |
| 189 | + # So just 1 instance of Example Response and Output Schema should be present |
| 190 | + assert tool.description.count("**Example Response:**") == 1, ( |
| 191 | + "The description should contain one example response" |
| 192 | + ) |
| 193 | + assert tool.description.count("**Output Schema:**") == 1, ( |
| 194 | + "The description should contain one full output schema" |
| 195 | + ) |
0 commit comments