|
1 | 1 | import base64
|
2 | 2 | from pathlib import Path
|
3 | 3 | from typing import TYPE_CHECKING
|
| 4 | +from unittest.mock import patch |
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 | from pydantic import AnyUrl
|
| 8 | +from starlette.routing import Mount, Route |
7 | 9 |
|
8 | 10 | from mcp.server.fastmcp import Context, FastMCP
|
9 | 11 | from mcp.server.fastmcp.prompts.base import EmbeddedResource, Message, UserMessage
|
@@ -31,6 +33,97 @@ async def test_create_server(self):
|
31 | 33 | assert mcp.name == "FastMCP"
|
32 | 34 | assert mcp.instructions == "Server instructions"
|
33 | 35 |
|
| 36 | + @pytest.mark.anyio |
| 37 | + async def test_normalize_path(self): |
| 38 | + """Test path normalization for mount paths.""" |
| 39 | + mcp = FastMCP() |
| 40 | + |
| 41 | + # Test root path |
| 42 | + assert mcp._normalize_path("/", "/messages/") == "/messages/" |
| 43 | + |
| 44 | + # Test path with trailing slash |
| 45 | + assert mcp._normalize_path("/github/", "/messages/") == "/github/messages/" |
| 46 | + |
| 47 | + # Test path without trailing slash |
| 48 | + assert mcp._normalize_path("/github", "/messages/") == "/github/messages/" |
| 49 | + |
| 50 | + # Test endpoint without leading slash |
| 51 | + assert mcp._normalize_path("/github", "messages/") == "/github/messages/" |
| 52 | + |
| 53 | + # Test both with trailing/leading slashes |
| 54 | + assert mcp._normalize_path("/api/", "/v1/") == "/api/v1/" |
| 55 | + |
| 56 | + @pytest.mark.anyio |
| 57 | + async def test_sse_app_with_mount_path(self): |
| 58 | + """Test SSE app creation with different mount paths.""" |
| 59 | + # Test with default mount path |
| 60 | + mcp = FastMCP() |
| 61 | + with patch.object( |
| 62 | + mcp, "_normalize_path", return_value="/messages/" |
| 63 | + ) as mock_normalize: |
| 64 | + mcp.sse_app() |
| 65 | + # Verify _normalize_path was called with correct args |
| 66 | + mock_normalize.assert_called_once_with("/", "/messages/") |
| 67 | + |
| 68 | + # Test with custom mount path in settings |
| 69 | + mcp = FastMCP() |
| 70 | + mcp.settings.mount_path = "/custom" |
| 71 | + with patch.object( |
| 72 | + mcp, "_normalize_path", return_value="/custom/messages/" |
| 73 | + ) as mock_normalize: |
| 74 | + mcp.sse_app() |
| 75 | + # Verify _normalize_path was called with correct args |
| 76 | + mock_normalize.assert_called_once_with("/custom", "/messages/") |
| 77 | + |
| 78 | + # Test with mount_path parameter |
| 79 | + mcp = FastMCP() |
| 80 | + with patch.object( |
| 81 | + mcp, "_normalize_path", return_value="/param/messages/" |
| 82 | + ) as mock_normalize: |
| 83 | + mcp.sse_app(mount_path="/param") |
| 84 | + # Verify _normalize_path was called with correct args |
| 85 | + mock_normalize.assert_called_once_with("/param", "/messages/") |
| 86 | + |
| 87 | + @pytest.mark.anyio |
| 88 | + async def test_starlette_routes_with_mount_path(self): |
| 89 | + """Test that Starlette routes are correctly configured with mount path.""" |
| 90 | + # Test with mount path in settings |
| 91 | + mcp = FastMCP() |
| 92 | + mcp.settings.mount_path = "/api" |
| 93 | + app = mcp.sse_app() |
| 94 | + |
| 95 | + # Find routes by type |
| 96 | + sse_routes = [r for r in app.routes if isinstance(r, Route)] |
| 97 | + mount_routes = [r for r in app.routes if isinstance(r, Mount)] |
| 98 | + |
| 99 | + # Verify routes exist |
| 100 | + assert len(sse_routes) == 1, "Should have one SSE route" |
| 101 | + assert len(mount_routes) == 1, "Should have one mount route" |
| 102 | + |
| 103 | + # Verify path values |
| 104 | + assert sse_routes[0].path == "/sse", "SSE route path should be /sse" |
| 105 | + assert ( |
| 106 | + mount_routes[0].path == "/messages" |
| 107 | + ), "Mount route path should be /messages" |
| 108 | + |
| 109 | + # Test with mount path as parameter |
| 110 | + mcp = FastMCP() |
| 111 | + app = mcp.sse_app(mount_path="/param") |
| 112 | + |
| 113 | + # Find routes by type |
| 114 | + sse_routes = [r for r in app.routes if isinstance(r, Route)] |
| 115 | + mount_routes = [r for r in app.routes if isinstance(r, Mount)] |
| 116 | + |
| 117 | + # Verify routes exist |
| 118 | + assert len(sse_routes) == 1, "Should have one SSE route" |
| 119 | + assert len(mount_routes) == 1, "Should have one mount route" |
| 120 | + |
| 121 | + # Verify path values |
| 122 | + assert sse_routes[0].path == "/sse", "SSE route path should be /sse" |
| 123 | + assert ( |
| 124 | + mount_routes[0].path == "/messages" |
| 125 | + ), "Mount route path should be /messages" |
| 126 | + |
34 | 127 | @pytest.mark.anyio
|
35 | 128 | async def test_non_ascii_description(self):
|
36 | 129 | """Test that FastMCP handles non-ASCII characters in descriptions correctly"""
|
@@ -518,8 +611,6 @@ async def async_tool(x: int, ctx: Context) -> str:
|
518 | 611 |
|
519 | 612 | @pytest.mark.anyio
|
520 | 613 | async def test_context_logging(self):
|
521 |
| - from unittest.mock import patch |
522 |
| - |
523 | 614 | import mcp.server.session
|
524 | 615 |
|
525 | 616 | """Test that context logging methods work."""
|
|
0 commit comments