File size: 992 Bytes
ff72ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# server.py
# A tiny mcp server example to show it working on a Huggingface Space
from fastmcp import FastMCP
from starlette.requests import Request
from starlette.responses import PlainTextResponse
import os

mcp = FastMCP("fastmcp-space")


@mcp.tool
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Basic dynamic resource returning a string
@mcp.resource("resource://greeting")
def get_greeting() -> str:
    """Provides a simple greeting message."""
    return "Hello from fastmcp-space"


@mcp.custom_route("/health", methods=["GET"])
async def health_check(request: Request) -> PlainTextResponse:
    return PlainTextResponse("OK")


# Export an ASGI app for uvicorn; choose a single path for Streamable HTTP (e.g. /mcp)
app = mcp.http_app(path="/mcp")

if __name__ == "__main__":
    # No uvicorn, just internal FastMCP server
    mcp.run(transport="http", host="0.0.0.0", port=int(os.getenv("PORT", 7860)), path="/mcp")