{"record":{"id":"801cfd835baf7d8a","repo":"PrefectHQ/fastmcp","slug":"error-building-request-for-self-route-method-upp","errorCode":null,"errorMessage":"Error building request for {self._route.method.upper()} {self._route.path}: {type(e).__name__}: {e}","messagePattern":"Error building request for (.+?) (.+?): (.+?): (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/providers/openapi/components.py","lineNumber":224,"sourceCode":"            # merged with the directed headers taking priority.\n            request = self._client.build_request(\n                method=directed_request.method,\n                url=str(directed_request.url.copy_with(query=None)),\n                params=list(directed_request.url.params.multi_items()),\n                headers=list(directed_request.headers.raw),\n                # read() materializes streaming bodies (multipart files=)\n                # that .content would refuse with RequestNotRead; idempotent\n                # for plain byte bodies.\n                content=directed_request.read(),\n            )\n\n            mcp_headers = get_http_headers()\n            if mcp_headers:\n                for key, value in mcp_headers.items():\n                    if key not in request.headers:\n                        request.headers[key] = value\n        except Exception as e:\n            raise ValueError(\n                f\"Error building request for {self._route.method.upper()} \"\n                f\"{self._route.path}: {type(e).__name__}: {e}\"\n            ) from e\n\n        # Send the request and process the response.\n        try:\n            logger.debug(\n                f\"run - sending request; headers: {_redact_headers(request.headers)}\"\n            )\n\n            response = await _send_request(self._client, request)\n            _raise_for_status(response)\n\n            # Try to parse as JSON first\n            try:\n                result = response.json()\n\n                # Handle structured content based on output schema","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/providers/openapi/components.py#L206-L242","documentation":"While constructing the outbound httpx request for an OpenAPI tool (mapping arguments into path/query/body/headers, JSON-encoding the body, merging MCP headers), any exception is caught and re-raised as ValueError('Error building request for METHOD path: ...') from the original error.","triggerScenarios":"Calling an OpenAPI tool with arguments that fail request construction: unserializable body values, wrong argument names, headers that violate httpx invariants, or a get_http_headers() failure.","commonSituations":"Passing non-JSON-serializable objects (datetime, bytes) in the tool arguments, mismatched parameter names vs the OpenAPI spec, custom header values containing invalid characters.","solutions":["Read the chained __cause__ to see the concrete failure","Fix the tool call arguments to match the OpenAPI schema (serializable JSON values, correct names)","Check custom route maps / parameter_map configuration","Inspect MCP headers set via context for invalid values"],"exampleFix":"// before\ntool({'due_date': datetime(2026, 1, 1)})\n// after\ntool({'due_date': '2026-01-01T00:00:00Z'})","handlingStrategy":"validation","validationCode":"import json\ndef validate_args(tool_schema: dict, args: dict) -> bool:\n    try:\n        json.dumps(args)\n    except (TypeError, ValueError):\n        return False\n    required = set(tool_schema.get('required', []))\n    return required.issubset(args)","typeGuard":null,"tryCatchPattern":"try:\n    result = await tool.run(args)\nexcept ValueError as e:\n    if str(e).startswith('Error building request'):\n        log.error('bad tool args: %s', e.__cause__)\n        raise TypeError('invalid arguments for tool') from e\n    raise","preventionTips":["Serialize all argument values to JSON-compatible types before calling","Match argument names to the OpenAPI parameter map exactly","Test each mapped route with representative arguments"],"tags":["openapi","request-building","validation"],"backgroundTag":"request-construction-failed","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}