-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (63 loc) · 2.35 KB
/
Copy pathserver.py
File metadata and controls
75 lines (63 loc) · 2.35 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from mcp.server.fastmcp import FastMCP
import logging
import sys
import os
import traceback
# Create an MCP server
mcp = FastMCP("Demo")
# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}!"
if __name__ == "__main__":
try:
# Ensure the logs directory exists
if not os.path.exists('logs'):
os.makedirs('logs')
# Configure logging with both file and terminal output
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('logs/server.log')
]
)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
for handler in logger.handlers:
handler.flush()
print("Starting server with logging enabled...")
logger.info("Starting MCP Demo server...")
logger.info("Available endpoints:")
logger.info(" - Tool: add(a: int, b: int)")
logger.info(" - Resource: /greeting/{name}")
logger.info("WebSocket Details:")
logger.info(" - WebSocket URL: ws://localhost:8765/ws")
logger.info(" - HTTP Server: http://localhost:8765")
logger.info(" - WebSocket Path: /ws")
logger.info(" - Protocol: WebSocket (ws://)")
logger.info("Connection Instructions:")
logger.info(" 1. Connect to the WebSocket endpoint at ws://localhost:8765/ws")
logger.info(" 2. Use the MCP client library for proper protocol handling")
logger.info(" 3. Example client connection: websocket_client('ws://localhost:8765/ws')")
for handler in logger.handlers:
handler.flush()
except Exception as e:
print(f"Error setting up logging: {str(e)}")
traceback.print_exc()
sys.exit(1)
try:
print("Starting MCP server...")
mcp.run()
except KeyboardInterrupt:
print("\nServer stopped by user")
except Exception as e:
print(f"Server error: {str(e)}")
traceback.print_exc()