Description
Hi there,
We are testing MCP tool calls in a machine remotely located in a data centre. Our server needs to make a search on the internet. Through MCP Inspector, all works as expected. However, when we run the client in terminal, we get error messages such as "Network Unreachable" if using requests or empty text if using httpx.AysncClient. We think the connection between the MCP server and client is fine when run in terminal, since local tools (e.g., addition) can be executed correctly. We suspect our client may be coded incorrectly, but based on the tutorials we can find so far, we are unable to identify the obvious errors in our code. So, we kind hope someone here can help out. Please find our test code below. Many thanks in advance.
The server (search_tool.py
):
from typing import Any, Dict, List
import httpx
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("search")
@mcp.tool()
async def search_wiki(entity: str, language: str ="en") -> str:
"""
Search the entity in WikiPedia and return the summary of the
required page, containing factual information about
the given entity.
Args:
entity (str): The entity to be searched.
language (str): The language for wikipedia.
Returns:
str: The search result. If the page corresponding to the entity
exists, return the summary of this entity in a string.
"""
params = {
'action': 'query',
'format': 'json',
'list': 'search',
'utf8': 1,
'srsearch': entity,
'srlimit': 5
}
api_url = f"https://{language}.wikipedia.org/w/api.php"
async with httpx.AsyncClient() as client:
response = await client.get(api_url, params=params, auth=None)
data = response.json()
results = []
for item in data.get('query', {}).get('search', []):
results.append({
'title': item.get('title', ''),
'snippet': item.get('snippet', ''),
'pageid': item.get('pageid', 0),
'wordcount': item.get('wordcount', 0),
'timestamp': item.get('timestamp', '')
})
return results
if __name__ == "__main__":
mcp.run(transport="stdio")`
The client:
import asyncio
from dotenv import load_dotenv
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import logging
logging.basicConfig(level=logging.INFO)
load_dotenv()
async def run_client():
server_params = StdioServerParameters(command="python", args=["search_tool.py"])
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = (await session.list_tools()).tools
for tool in tools:
logging.info(tool.name)
res = await session.call_tool(tool.name, {"entity": "Gaussian process"})
logging.info(res)
if __name__ == "__main__":
asyncio.run(run_client())