-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzcp_server_template.py
More file actions
104 lines (86 loc) · 3.03 KB
/
Copy pathzcp_server_template.py
File metadata and controls
104 lines (86 loc) · 3.03 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
"""Server template for a user-implemented ZCP backend.
Run with:
uvicorn examples.zcp_server_template:application --host 0.0.0.0 --port 8000
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
from zcp import (
AuthProfile,
BearerAuthConfig,
FastZCP,
PromptArgument,
RateLimitConfig,
ZCPServerConfig,
create_asgi_app,
)
app = FastZCP(
"Weather Backend Template",
version="1.0.0",
instructions="Example user-owned ZCP backend with tools, resources, prompts, completions, tasks, and auth metadata.",
auth_profile=AuthProfile(
issuer=os.environ.get("ZCP_AUTH_ISSUER", "https://auth.example.com"),
authorization_url=os.environ.get("ZCP_AUTHORIZATION_URL", "https://auth.example.com/oauth/authorize"),
token_url=os.environ.get("ZCP_TOKEN_URL", "https://auth.example.com/oauth/token"),
scopes=["weather.read", "weather.admin"],
),
)
@app.tool(
name="weather.get_current",
description="Get the current weather for a city.",
input_schema={
"type": "object",
"properties": {
"city": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["city"],
"additionalProperties": False,
},
output_mode="scalar",
inline_ok=True,
required_scopes=("weather.read",),
)
def get_weather(city: str, unit: str = "celsius", ctx=None):
return {"city": city, "unit": unit, "temperature": 24, "condition": "Cloudy", "humidity": 67}
@app.resource(
"weather://cities",
name="Supported Cities",
mime_type="application/json",
required_scopes=("weather.read",),
)
def supported_cities():
return ["Hangzhou", "Beijing", "Shanghai", "Shenzhen"]
@app.prompt(
name="weather.summary",
description="Build a user-facing weather summary prompt.",
arguments=[PromptArgument(name="city", required=True), PromptArgument(name="temperature")],
required_scopes=("weather.read",),
)
def weather_prompt(city: str, temperature: str | None = None):
return [
{"role": "system", "content": "You summarize weather clearly and briefly."},
{"role": "user", "content": f"Summarize the weather for {city}. Temperature: {temperature or 'unknown'}."},
]
@app.completion("city")
def complete_city(request):
names = ["Hangzhou", "Beijing", "Shanghai", "Shenzhen"]
return [item for item in names if item.lower().startswith(request.value.lower())]
@app.task("weather.refresh")
def refresh_weather(payload):
return {"status": "refreshed", "city": payload["city"]}
application = create_asgi_app(
app,
config=ZCPServerConfig(
service_name="zcp-weather",
environment="production",
auth=BearerAuthConfig(token=os.environ.get("ZCP_BEARER_TOKEN", "demo-token")),
rate_limit=RateLimitConfig(window_seconds=60, max_requests=240),
),
)