-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
44 lines (33 loc) · 1.02 KB
/
Copy pathserver.py
File metadata and controls
44 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from mcp.server.fastmcp import FastMCP
from tavily import TavilyClient
from dotenv import load_dotenv
from typing import Dict, List
import os
load_dotenv()
if "TAVILY_API_KEY" not in os.environ:
raise Exception("TAVILY_API_KEY environment variable not set")
# Tavily API key
TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
# Initialize Tavily client
tavily_client = TavilyClient(TAVILY_API_KEY)
PORT = os.environ.get("PORT", 10000)
# Create an MCP server
mcp = FastMCP("web-search", host="0.0.0.0", port=PORT)
# Add a tool that uses Tavily
@mcp.tool()
def web_search(query: str) -> List[Dict]:
"""
Use this tool to search the web for information.
Args:
query: The search query.
Returns:
The search results.
"""
try:
response = tavily_client.search(query)
return response["results"]
except Exception as e:
return "Error: " + str(e)
# Run the server
if __name__ == "__main__":
mcp.run(transport="streamable-http")