|
| 1 | +import inspect |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
1 | 5 | import pytest |
| 6 | +from pydantic import ValidationError |
2 | 7 |
|
3 | 8 | from fastmcp.cli.run import ( |
| 9 | + create_mcp_config_server, |
4 | 10 | import_server, |
5 | 11 | is_url, |
6 | 12 | parse_file_path, |
7 | 13 | ) |
| 14 | +from fastmcp.client.client import Client |
| 15 | +from fastmcp.client.transports import FastMCPTransport |
| 16 | +from fastmcp.mcp_config import MCPConfig, StdioMCPServer |
| 17 | +from fastmcp.server.server import FastMCP |
8 | 18 |
|
9 | 19 |
|
10 | 20 | class TestUrlDetection: |
@@ -80,6 +90,57 @@ def test_parse_file_path_directory(self, tmp_path): |
80 | 90 | assert exc_info.value.code == 1 |
81 | 91 |
|
82 | 92 |
|
| 93 | +class TestMCPConfig: |
| 94 | + """Test MCPConfig functionality.""" |
| 95 | + |
| 96 | + async def test_run_mcp_config(self, tmp_path: Path): |
| 97 | + """Test creating a server from an MCPConfig file.""" |
| 98 | + server_script = inspect.cleandoc(""" |
| 99 | + from fastmcp import FastMCP |
| 100 | +
|
| 101 | + mcp = FastMCP() |
| 102 | +
|
| 103 | + @mcp.tool |
| 104 | + def add(a: int, b: int) -> int: |
| 105 | + return a + b |
| 106 | +
|
| 107 | + if __name__ == '__main__': |
| 108 | + mcp.run() |
| 109 | + """) |
| 110 | + |
| 111 | + script_path: Path = tmp_path / "test.py" |
| 112 | + script_path.write_text(server_script) |
| 113 | + |
| 114 | + mcp_config_path = tmp_path / "mcp_config.json" |
| 115 | + |
| 116 | + mcp_config = MCPConfig( |
| 117 | + mcpServers={ |
| 118 | + "test_server": StdioMCPServer(command="python", args=[str(script_path)]) |
| 119 | + } |
| 120 | + ) |
| 121 | + mcp_config.write_to_file(mcp_config_path) |
| 122 | + |
| 123 | + server: FastMCP[None] = create_mcp_config_server(mcp_config_path) |
| 124 | + |
| 125 | + client = Client[FastMCPTransport](server) |
| 126 | + |
| 127 | + async with client: |
| 128 | + tools = await client.list_tools() |
| 129 | + assert len(tools) == 1 |
| 130 | + |
| 131 | + async def test_validate_mcp_config(self, tmp_path: Path): |
| 132 | + """Test creating a server from an MCPConfig file.""" |
| 133 | + |
| 134 | + mcp_config_path = tmp_path / "mcp_config.json" |
| 135 | + |
| 136 | + mcp_config = {"mcpServers": {"test_server": dict(x=1, y=2)}} |
| 137 | + with mcp_config_path.open("w") as f: |
| 138 | + json.dump(mcp_config, f) |
| 139 | + |
| 140 | + with pytest.raises(ValidationError, match="validation errors for MCPConfig"): |
| 141 | + create_mcp_config_server(mcp_config_path) |
| 142 | + |
| 143 | + |
83 | 144 | class TestServerImport: |
84 | 145 | """Test server import functionality using real files.""" |
85 | 146 |
|
|
0 commit comments