fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

Added feature : Load MCP server using config

Open sandipan1 opened this issue 7 months ago • 1 comments

The current Client supports loading server from ClientTransport , FastMCP server , script path, URL . It does not support config dictionary to add a server which is a standard way in many clients . I have added this feature for adding a single server through config dictionary.

Example client.py

from fastmcp import Client
import asyncio
config = {
  "mcpServers": {
    "time": {
      "command": "uvx",
      "args": ["mcp-server-time", "--local-timezone=Asia/Kolkata"]
    }
  }
}
client = Client(config)
print(client.transport.args)
async def main():
    async with client:
        print(f"Client connected: {client.is_connected()}")

        tools = await client.list_tools()
        
        print(f"Available tools: {[tool.name for tool in tools]}")
      
        if any(tool.name == "get_current_time" for tool in tools):
            result = await client.call_tool("get_current_time", {"timezone": "Asia/Kolkata"})
            print(f"Time result: {result[0].text}")

    print(f"Client connected: {client.is_connected()}")

if __name__ == "__main__":
    asyncio.run(main())

Output :

['mcp-server-time', '--local-timezone=Asia/Kolkata']
Client connected: True
Available tools: ['get_current_time', 'convert_time']
Time result: {
  "timezone": "Asia/Kolkata",
  "datetime": "2025-04-26T02:11:10+05:30",
  "is_dst": false
}
Client connected: False

sandipan1 avatar Apr 25 '25 20:04 sandipan1

Thanks! Great idea. I think this just needs some tests to ensure it works, nothing complex.

jlowin avatar Apr 26 '25 01:04 jlowin

Hey @jlowin I have run a few example server tests on my local. Please let me know how to be more comprehensive about it

sandipan1 avatar Apr 26 '25 06:04 sandipan1