Skip to main content
Glama
basic_server.py2.44 kB
"""Basic MCP server example using the hero framework.""" import sys from pathlib import Path # Add parent directory to path for imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) import anyio import mcp.types as types from mcp.server.stdio import stdio_server from mcp_server_hero.core.server import MCPServerHero async def add_numbers(a: int, b: int) -> int: """Add two numbers together.""" return a + b async def get_greeting() -> str: """Get a friendly greeting message.""" return "Hello from MCP Server Hero!" async def create_summary_prompt(topic: str, style: str = "professional") -> str: """Create a summary prompt for the given topic.""" styles = { "professional": "Please provide a professional and detailed summary", "casual": "Please give a casual and friendly summary", "technical": "Please provide a technical and precise summary", } selected_style = styles.get(style, styles["professional"]) return f"{selected_style} of the following topic: {topic}" async def main() -> None: """Run the basic MCP server example.""" # Create server instance server = MCPServerHero(name="Basic Example Server", debug=True) # Register a tool server.add_tool( name="add", tool_func=add_numbers, description="Add two numbers together", schema={ "type": "object", "properties": { "a": {"type": "integer", "description": "First number"}, "b": {"type": "integer", "description": "Second number"}, }, "required": ["a", "b"], }, ) # Register a resource server.add_resource( uri="greeting://hello", resource_func=get_greeting, name="greeting", description="A friendly greeting message", ) # Register a prompt server.add_prompt( name="summary", prompt_func=create_summary_prompt, description="Create a summary prompt for any topic", arguments=[ {"name": "topic", "description": "Topic to summarize", "required": True}, {"name": "style", "description": "Summary style", "required": False}, ], ) # Run the server async with stdio_server() as streams: await server.run(streams[0], streams[1], server.create_initialization_options()) if __name__ == "__main__": anyio.run(main)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/namnd00/mcp-server-hero'

If you have feedback or need assistance with the MCP directory API, please join our Discord server