MCPcopy
hub / github.com/CopilotKit/CopilotKit / handler

Function handler

sdk-python/copilotkit/integrations/fastapi.py:77–170  ·  view source on GitHub ↗

Handle FastAPI request

(request: Request, sdk: CopilotKitRemoteEndpoint)

Source from the content-addressed store, hash-verified

75
76
77async def handler(request: Request, sdk: CopilotKitRemoteEndpoint):
78 """Handle FastAPI request"""
79
80 try:
81 body = await request.json()
82 except: # pylint: disable=bare-except
83 body = None
84
85 # Propagate x-aimock-* headers to outgoing LLM calls via ContextVar
86 set_forwarded_headers(dict(request.headers))
87
88 path = request.path_params.get("path")
89 method = request.method
90 context = cast(
91 CopilotKitContext,
92 {
93 "properties": (body or {}).get("properties", {}),
94 "frontend_url": (body or {}).get("frontendUrl", None),
95 "headers": request.headers,
96 },
97 )
98
99 # handle / request for info endpoint
100 if method in ["GET", "POST"] and path == "":
101 accept_header = request.headers.get("accept", "")
102 return await handle_info(
103 sdk=sdk,
104 context=context,
105 as_html="text/html" in accept_header,
106 )
107
108 # handle /agent/name request for executing an agent
109 if method == "POST" and (match := re.match(r"agent/([a-zA-Z0-9_-]+)", path)):
110 name = match.group(1)
111 body = body or {}
112
113 thread_id = body.get("threadId", str(uuid.uuid4()))
114 state = body.get("state", {})
115 messages = body.get("messages", [])
116 actions = body.get("actions", [])
117
118 # used for LangGraph only
119 node_name = body.get("nodeName")
120
121 return handle_execute_agent(
122 sdk=sdk,
123 context=context,
124 thread_id=thread_id,
125 node_name=node_name,
126 name=name,
127 state=state,
128 messages=messages,
129 actions=actions,
130 )
131
132 # handle /agent/name/state request for getting agent state
133 if method == "POST" and (match := re.match(r"agent/([a-zA-Z0-9_-]+)/state", path)):
134 name = match.group(1)

Callers 6

run_handler_in_threadFunction · 0.70
make_handlerFunction · 0.70
wrap_model_callMethod · 0.50
awrap_model_callMethod · 0.50
wrap_tool_callMethod · 0.50
awrap_tool_callMethod · 0.50

Calls 10

handle_infoFunction · 0.85
strFunction · 0.85
handle_execute_agentFunction · 0.85
body_get_or_raiseFunction · 0.85
handle_get_agent_stateFunction · 0.85
handle_execute_actionFunction · 0.85
handler_v1Function · 0.85
jsonMethod · 0.65
getMethod · 0.65
set_forwarded_headersFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…