MCPcopy Index your code
hub / github.com/msgspec/msgspec / handle_request

Method handle_request

examples/asyncio-kv/kv.py:92–111  ·  view source on GitHub ↗

Handle a single request and return the result (if any)

(self, req: Request)

Source from the content-addressed store, hash-verified

90 return
91
92 async def handle_request(self, req: Request) -> Any:
93 """Handle a single request and return the result (if any)"""
94 # We use pattern matching here to branch on the different message types.
95 # You could just as well use an if-else statement, but pattern matching
96 # works pretty well here.
97 match req:
98 case Get(key):
99 # Return the value for a key, or None if missing
100 return self.kv.get(key)
101 case Put(key, val):
102 # Add a new key-value pair
103 self.kv[key] = val
104 return None
105 case Del(key):
106 # Remove a key-value pair if it exists
107 self.kv.pop(key, None)
108 return None
109 case ListKeys():
110 # Return a list of all keys in the store
111 return sorted(self.kv)
112
113 async def serve_forever(self) -> None:
114 server = await asyncio.start_server(

Callers 1

handle_connectionMethod · 0.95

Calls 1

getMethod · 0.80

Tested by

no test coverage detected