server.py•2.14 kB
from fastmcp import FastMCP
from datetime import datetime
# Create the MCP server
mcp = FastMCP(
name = 'HelloWorldServer',
instructions = 'A simple hello world MCP server with resources'
)
# Your existing tools
@mcp.tool
def say_hello(name: str) -> str:
'''Say hello to someone'''
return f'Hello, {name}! Welcome to FastMCP!'
@mcp.tool
def greet_with_age(name: str, age: int) -> str:
'''Greet someone and mention their age'''
return f'Hello {name}! You are {age} years old.'
# Add a static resource
@mcp.resource('config://server-info')
def get_server_info():
'''Get server configuration and status'''
return {
'server_name': 'HelloWorldServer',
'version': '1.0.0',
'status': 'running',
'timestamp': datetime.now().isoformat()
}
# Add another static resource
@mcp.resource('data://greetings')
def get_greeting_templates():
'''Get available greeting templates'''
return {
'templates': [
'Hello, {name}!',
'Welcome, {name}!',
'Greetings, {name}!',
'Hi there, {name}!'
],
'languages': ['en', 'es', 'fr'],
'default': 'Hello, {name}!'
}
# Add a resource template (dynamic resource)
@mcp.resource('users://{user_id}/profile')
def get_user_profile(user_id: str):
'''Get user profile by ID'''
# In real app, this would fetch from database
return {
'id': user_id,
'name': f'User {user_id}',
'joined': '2024-01-15',
'greeting_count': 42,
'preferred_greeting': 'Hello'
}
# Add another resource template
@mcp.resource('stats://{stat_type}')
def get_statistics(stat_type: str):
'''Get statistics by type'''
stats = {
'greetings': {
'total': 1234,
'today': 56,
'most_popular': 'Hello'
},
'users': {
'total': 89,
'active': 45,
'new_today': 3
}
}
return stats.get(stat_type, {'error': f'Unknown stat type: {stat_type}'})
if __name__ == '__main__':
mcp.run(transport='http', host='127.0.0.1', port=8000)