From 06b9b98b6ce81a67dfeaefc71c799171924fef9b Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Thu, 22 May 2025 11:22:45 -0400 Subject: [PATCH] Raise an error if a Client is created with no servers in config --- src/fastmcp/client/transports.py | 6 +++++- tests/client/test_client.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.py index e2ae60816..e2e8a7eb5 100644 --- a/src/fastmcp/client/transports.py +++ b/src/fastmcp/client/transports.py @@ -528,8 +528,12 @@ class MCPConfigTransport(ClientTransport): config = MCPConfig.from_dict(config) self.config = config + # if there are no servers, raise an error + if len(self.config.mcpServers) == 0: + raise ValueError("No MCP servers defined in the config") + # if there's exactly one server, create a client for that server - if len(self.config.mcpServers) == 1: + elif len(self.config.mcpServers) == 1: self.transport = list(self.config.mcpServers.values())[0].to_transport() # otherwise create a composite client diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 62bddf0ab..e85ca4fc5 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -673,6 +673,18 @@ class TestInferTransport: assert transport.transport.command == "echo" assert transport.transport.args == ["hello"] + def test_config_with_no_servers(self): + """Test that an empty MCPConfig raises a ValueError.""" + config = {"mcpServers": {}} + with pytest.raises(ValueError, match="No MCP servers defined in the config"): + infer_transport(config) + + def test_mcpconfigtransport_with_no_servers(self): + """Test that MCPConfigTransport raises a ValueError when initialized with an empty config.""" + config = {"mcpServers": {}} + with pytest.raises(ValueError, match="No MCP servers defined in the config"): + MCPConfigTransport(config=config) + def test_infer_composite_client(config): config = { "mcpServers": {