test_mcp.py•3.79 kB
"""
Test script for MCP tools.
This script tests the MCP tools by making requests to the MCP server.
"""
import requests
import json
import sys
def list_mcp_tools():
"""List the available tools in the MCP server."""
response = requests.post(
"http://localhost:8000/mcp/jsonrpc",
json={"jsonrpc": "2.0", "id": 1, "method": "mcp.list_tools"},
)
if response.status_code == 200:
print("Available MCP tools:")
for tool in response.json()["result"]["tools"]:
print(f"- {tool['name']}: {tool['description']}")
else:
print(f"Error: {response.text}")
def test_search_photos():
"""Test the search_photos tool."""
response = requests.post(
"http://localhost:8000/mcp/jsonrpc",
json={
"jsonrpc": "2.0",
"id": 2,
"method": "mcp.call_tool",
"params": {
"name": "search_photos",
"arguments": {"query": "mountains", "per_page": 3},
},
},
)
if response.status_code == 200:
result = response.json()
if "error" in result:
print(f"Error: {result['error']}")
else:
print(f"Search photos result: {len(result['result'])} photos found")
print(
json.dumps(result["result"], indent=2)[:500] + "..."
) # Print first 500 chars
else:
print(f"Error: {response.text}")
def test_get_photos():
"""Test the get_photos tool."""
response = requests.post(
"http://localhost:8000/mcp/jsonrpc",
json={
"jsonrpc": "2.0",
"id": 3,
"method": "mcp.call_tool",
"params": {
"name": "get_photos",
"arguments": {"per_page": 3, "order_by": "popular"},
},
},
)
if response.status_code == 200:
result = response.json()
if "error" in result:
print(f"Error: {result['error']}")
else:
print(f"Get photos result: {len(result['result'])} photos found")
print(
json.dumps(result["result"], indent=2)[:500] + "..."
) # Print first 500 chars
else:
print(f"Error: {response.text}")
def test_get_random_photos():
"""Test the get_random_photos tool."""
response = requests.post(
"http://localhost:8000/mcp/jsonrpc",
json={
"jsonrpc": "2.0",
"id": 4,
"method": "mcp.call_tool",
"params": {
"name": "get_random_photos",
"arguments": {"query": "forest", "count": 2},
},
},
)
if response.status_code == 200:
result = response.json()
if "error" in result:
print(f"Error: {result['error']}")
else:
print(f"Get random photos result: {len(result['result'])} photos found")
print(
json.dumps(result["result"], indent=2)[:500] + "..."
) # Print first 500 chars
else:
print(f"Error: {response.text}")
if __name__ == "__main__":
# Check if a specific test was requested
if len(sys.argv) > 1:
test_name = sys.argv[1]
if test_name == "list":
list_mcp_tools()
elif test_name == "search":
test_search_photos()
elif test_name == "photos":
test_get_photos()
elif test_name == "random":
test_get_random_photos()
else:
print(f"Unknown test: {test_name}")
print("Available tests: list, search, photos, random")
else:
# Run all tests
list_mcp_tools()
test_search_photos()
test_get_photos()
test_get_random_photos()