@@ -60,7 +60,11 @@ def _capture_headers(self) -> None:
6060 if len (request_log ) > 10 :
6161 request_log .pop (0 )
6262
63- def do_POST (self ) -> None : # pylint: disable=invalid-name
63+ def do_POST (
64+ self ,
65+ ) -> (
66+ None
67+ ): # pylint: disable=invalid-name,too-many-locals,too-many-branches,too-many-statements
6468 """Handle POST requests (MCP protocol endpoints)."""
6569 self ._capture_headers ()
6670
@@ -77,23 +81,40 @@ def do_POST(self) -> None: # pylint: disable=invalid-name
7781 request_id = 1
7882 method = "unknown"
7983
84+ # Log the RPC method in the request log
85+ if request_log :
86+ request_log [- 1 ]["rpc_method" ] = method
87+
8088 # Determine tool name based on authorization header to avoid collisions
8189 auth_header = self .headers .get ("Authorization" , "" )
8290
8391 # Initialize tool info defaults
8492 tool_name = "mock_tool_no_auth"
8593 tool_desc = "Mock tool with no authorization"
94+ error_mode = False
8695
8796 # Match based on token content
88- if "test-secret-token" in auth_header :
89- tool_name = "mock_tool_file"
90- tool_desc = "Mock tool with file-based auth"
91- elif "my-k8s-token" in auth_header :
92- tool_name = "mock_tool_k8s"
93- tool_desc = "Mock tool with Kubernetes token"
94- elif "my-client-token" in auth_header :
95- tool_name = "mock_tool_client"
96- tool_desc = "Mock tool with client-provided token"
97+ match True :
98+ case _ if "test-secret-token" in auth_header :
99+ tool_name = "mock_tool_file"
100+ tool_desc = "Mock tool with file-based auth"
101+ case _ if "my-k8s-token" in auth_header :
102+ tool_name = "mock_tool_k8s"
103+ tool_desc = "Mock tool with Kubernetes token"
104+ case _ if "my-client-token" in auth_header :
105+ tool_name = "mock_tool_client"
106+ tool_desc = "Mock tool with client-provided token"
107+ case _ if "error-mode" in auth_header :
108+ tool_name = "mock_tool_error"
109+ tool_desc = "Mock tool configured to return errors"
110+ error_mode = True
111+ case _:
112+ # Default case already set above
113+ pass
114+
115+ # Log the tool name in the request log
116+ if request_log :
117+ request_log [- 1 ]["tool_name" ] = tool_name
97118
98119 # Handle MCP protocol methods using match statement
99120 response : dict = {}
@@ -145,29 +166,46 @@ def do_POST(self) -> None: # pylint: disable=invalid-name
145166 tool_called = params .get ("name" , "unknown" )
146167 arguments = params .get ("arguments" , {})
147168
148- # Build result text
149- auth_preview = (
150- auth_header [:50 ] if len (auth_header ) > 50 else auth_header
151- )
152- result_text = (
153- f"Mock tool '{ tool_called } ' executed successfully "
154- f"with arguments: { arguments } . Auth used: { auth_preview } ..."
155- )
156-
157- # Return successful tool execution result
158- response = {
159- "jsonrpc" : "2.0" ,
160- "id" : request_id ,
161- "result" : {
162- "content" : [
163- {
164- "type" : "text" ,
165- "text" : result_text ,
166- }
167- ],
168- "isError" : False ,
169- },
170- }
169+ # Check if error mode is enabled
170+ if error_mode :
171+ # Return error response
172+ response = {
173+ "jsonrpc" : "2.0" ,
174+ "id" : request_id ,
175+ "result" : {
176+ "content" : [
177+ {
178+ "type" : "text" ,
179+ "text" : (
180+ f"Error: Tool '{ tool_called } ' "
181+ "execution failed - simulated error."
182+ ),
183+ }
184+ ],
185+ "isError" : True ,
186+ },
187+ }
188+ else :
189+ # Build result text
190+ result_text = (
191+ f"Mock tool '{ tool_called } ' executed successfully "
192+ f"with arguments: { arguments } ."
193+ )
194+
195+ # Return successful tool execution result
196+ response = {
197+ "jsonrpc" : "2.0" ,
198+ "id" : request_id ,
199+ "result" : {
200+ "content" : [
201+ {
202+ "type" : "text" ,
203+ "text" : result_text ,
204+ }
205+ ],
206+ "isError" : False ,
207+ },
208+ }
171209
172210 case _:
173211 # Generic success response for other methods
@@ -194,6 +232,11 @@ def do_GET(self) -> None: # pylint: disable=invalid-name
194232 )
195233 case "/debug/requests" :
196234 self ._send_json_response (request_log )
235+ case "/debug/clear" :
236+ # Clear the request log and last captured headers
237+ request_log .clear ()
238+ last_headers .clear ()
239+ self ._send_json_response ({"status" : "cleared" , "request_count" : 0 })
197240 case "/" :
198241 self ._send_help_page ()
199242 case _:
0 commit comments