blob: a32989703541710e2abfb81ce58df1a3cbf208fe (
plain)
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
76
77
78
79
80
81
82
83
84
|
import logging
import os
import sys
from mcp.server.fastmcp import FastMCP
# reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
if sys.platform == "win32" and os.environ.get('PYTHONIOENCODING') is None:
sys.stdin.reconfigure(encoding="utf-8")
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
logger = logging.getLogger('mcp_hello_world_server')
# Simple prompt template
GREETING_PROMPT_TEMPLATE = """
Hello there! You've chosen to use the greeting prompt with the name: {name}.
This is a simple demonstration of how MCP prompts work. You can use this as a
starting point
for creating your own MCP servers with more complex functionality.
Here's what this Hello World MCP server can do:
1. Provide a greeting prompt (which you're using now)
2. Offer a simple "hello" tool that returns a greeting message
3. Expose a static resource with a welcome message
Feel free to explore these capabilities and use this server as a template
for building more advanced MCP servers!
"""
class HelloWorldServer:
def __init__(self) -> None:
self.welcome_message = "Welcome to the Hello World MCP Server! " \
"This is a simple static resource."
def get_greeting(self, name: str) -> str:
"""Generate a greeting message for the given name"""
return f"Hello, {name}! Welcome to the Hello World MCP Server."
def create_server(host: str = "127.0.0.1", port: int = 8080) -> FastMCP:
"""Create and configure the FastMCP server."""
# Create a FastMCP server with host and port settings
mcp = FastMCP("Hello World", host=host, port=port)
# Create a HelloWorldServer instance
hello_world = HelloWorldServer()
# Add a static resource
@mcp.resource("hello://welcome")
def welcome_resource() -> str:
"""A simple welcome message resource"""
return hello_world.welcome_message
# Add a greeting tool
@mcp.tool()
def hello(name: str) -> str:
"""Get a personalized greeting message"""
return hello_world.get_greeting(name)
# Add a greeting prompt
@mcp.prompt()
def greeting(name: str) -> str:
"""A simple greeting prompt that demonstrates MCP functionality"""
return GREETING_PROMPT_TEMPLATE.format(name=name)
return mcp
async def main(transport_type: str, host: str, port: int) -> None:
"""Start the server with the specified transport."""
logger.info("Starting MCP Hello World Server")
logger.info(f"Starting Hello World MCP Server with {transport_type} transport")
# Create the server with host and port
mcp = create_server(host=host, port=port)
# Run the server with the appropriate transport
if transport_type == "stdio":
logger.info("Server running with stdio transport")
await mcp.run_stdio_async()
else: # remote transport
logger.info(f"Server running with remote transport on {host}:{port}")
await mcp.run_sse_async()
|