Spaces:
Sleeping
Sleeping
update
Browse files- server.py +13 -4
- {static → templates}/index.html +11 -11
server.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import contextlib
|
2 |
-
from fastapi import FastAPI
|
3 |
from fastapi.responses import HTMLResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
|
|
5 |
from echo_server import mcp as echo_mcp
|
6 |
from math_server import mcp as math_mcp
|
7 |
import os
|
@@ -18,16 +19,24 @@ async def lifespan(app: FastAPI):
|
|
18 |
|
19 |
BASE_DIR = os.path.dirname(__file__)
|
20 |
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
|
|
21 |
|
22 |
app = FastAPI(lifespan=lifespan)
|
23 |
|
24 |
-
# Serve static assets (screenshot, styles)
|
25 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
26 |
|
|
|
|
|
27 |
|
28 |
@app.get("/", response_class=HTMLResponse)
|
29 |
-
async def index():
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
|
33 |
app.mount("/echo", echo_mcp.streamable_http_app())
|
|
|
1 |
import contextlib
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
from fastapi.responses import HTMLResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi.templating import Jinja2Templates
|
6 |
from echo_server import mcp as echo_mcp
|
7 |
from math_server import mcp as math_mcp
|
8 |
import os
|
|
|
19 |
|
20 |
BASE_DIR = os.path.dirname(__file__)
|
21 |
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
22 |
+
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
|
23 |
|
24 |
app = FastAPI(lifespan=lifespan)
|
25 |
|
26 |
+
# Serve static assets (screenshot, styles)
|
27 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
28 |
|
29 |
+
templates = Jinja2Templates(directory=TEMPLATES_DIR)
|
30 |
+
|
31 |
|
32 |
@app.get("/", response_class=HTMLResponse)
|
33 |
+
async def index(request: Request):
|
34 |
+
space_host = os.environ.get("SPACE_HOST")
|
35 |
+
if space_host and space_host.strip():
|
36 |
+
base_url = space_host.strip().rstrip("/")
|
37 |
+
else:
|
38 |
+
base_url = f"{request.url.scheme}://{request.url.netloc}"
|
39 |
+
return templates.TemplateResponse("index.html", {"request": request, "base_url": base_url})
|
40 |
|
41 |
|
42 |
app.mount("/echo", echo_mcp.streamable_http_app())
|
{static → templates}/index.html
RENAMED
@@ -52,13 +52,13 @@
|
|
52 |
<div class="tile">
|
53 |
<h3>Available servers</h3>
|
54 |
<ul class="muted">
|
55 |
-
<li><a href="/echo"
|
56 |
-
<li><a href="/math"
|
57 |
</ul>
|
58 |
</div>
|
59 |
<div class="tile">
|
60 |
<h3>Base URL</h3>
|
61 |
-
<p class="muted">Each server lives at <code>{base_URL}
|
62 |
</div>
|
63 |
</div>
|
64 |
|
@@ -71,10 +71,10 @@
|
|
71 |
|
72 |
<pre><code>Example
|
73 |
|
74 |
-
base_URL =
|
75 |
|
76 |
-
Echo MCP = {
|
77 |
-
Math MCP = {
|
78 |
</code></pre>
|
79 |
|
80 |
<p class="muted" style="margin:10px 0 8px">Illustration of the “Embed this Space” dialog:</p>
|
@@ -85,20 +85,20 @@ Math MCP = {base_URL}/math
|
|
85 |
<div class="card">
|
86 |
<h3 style="margin-top:0">Quick links</h3>
|
87 |
<ul>
|
88 |
-
<li><a href="/echo">Open /echo</a></li>
|
89 |
-
<li><a href="/math">Open /math</a></li>
|
90 |
</ul>
|
91 |
<h3>Use in clients</h3>
|
92 |
<p class="muted">Point your MCP client to the endpoints below:</p>
|
93 |
<pre><code>HTTP streaming endpoints
|
94 |
|
95 |
-
Echo: GET {
|
96 |
-
Math: GET {
|
97 |
</code></pre>
|
98 |
</div>
|
99 |
</div>
|
100 |
|
101 |
-
<p class="footer">
|
102 |
</div>
|
103 |
</body>
|
104 |
</html>
|
|
|
52 |
<div class="tile">
|
53 |
<h3>Available servers</h3>
|
54 |
<ul class="muted">
|
55 |
+
<li><a href="{{ base_url }}/echo" target="_blank" rel="noopener noreferrer">{{ base_url }}/echo</a> — Echo MCP server</li>
|
56 |
+
<li><a href="{{ base_url }}/math" target="_blank" rel="noopener noreferrer">{{ base_url }}/math</a> — Math MCP server</li>
|
57 |
</ul>
|
58 |
</div>
|
59 |
<div class="tile">
|
60 |
<h3>Base URL</h3>
|
61 |
+
<p class="muted">Each server lives at <code>{{ "{{ base_URL }}" }}</code> → here, resolved to <code>{{ base_url }}</code>. Replace it with your Space's origin if needed.</p>
|
62 |
</div>
|
63 |
</div>
|
64 |
|
|
|
71 |
|
72 |
<pre><code>Example
|
73 |
|
74 |
+
base_URL = {{ base_url }}
|
75 |
|
76 |
+
Echo MCP = {{ base_url }}/echo
|
77 |
+
Math MCP = {{ base_url }}/math
|
78 |
</code></pre>
|
79 |
|
80 |
<p class="muted" style="margin:10px 0 8px">Illustration of the “Embed this Space” dialog:</p>
|
|
|
85 |
<div class="card">
|
86 |
<h3 style="margin-top:0">Quick links</h3>
|
87 |
<ul>
|
88 |
+
<li><a href="{{ base_url }}/echo" target="_blank" rel="noopener noreferrer">Open /echo</a></li>
|
89 |
+
<li><a href="{{ base_url }}/math" target="_blank" rel="noopener noreferrer">Open /math</a></li>
|
90 |
</ul>
|
91 |
<h3>Use in clients</h3>
|
92 |
<p class="muted">Point your MCP client to the endpoints below:</p>
|
93 |
<pre><code>HTTP streaming endpoints
|
94 |
|
95 |
+
Echo: GET {{ base_url }}/echo
|
96 |
+
Math: GET {{ base_url }}/math
|
97 |
</code></pre>
|
98 |
</div>
|
99 |
</div>
|
100 |
|
101 |
+
<p class="footer"> Credit: technique discovered via <a href="https://youtu.be/wXAqv8uvY0M" target="_blank" rel="noopener noreferrer">this video</a>.</p>
|
102 |
</div>
|
103 |
</body>
|
104 |
</html>
|