Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcp/client/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,4 @@ async def handle_token_response_scopes(
token_response = OAuthToken.model_validate_json(content)
return token_response
except ValidationError as e: # pragma: no cover
raise OAuthTokenError(f"Invalid token response: {e}")
raise OAuthTokenError(f"Invalid token response: {e}") from e
2 changes: 1 addition & 1 deletion src/mcp/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ def _output_schema_validator(self, name: str, output_schema: dict[str, Any]) ->
try:
validator_cls.check_schema(output_schema)
except SchemaError as e:
raise RuntimeError(f"Invalid schema for tool {name}: {e}")
raise RuntimeError(f"Invalid schema for tool {name}: {e}") from e
# jsonschema ships no `py.typed`, so pyright reads typeshed's stub, which declares
# `registry` as required (concrete validators default it); cast to a schema-only ctor.
validator = cast("Callable[[dict[str, Any]], Validator]", validator_cls)(output_schema)
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/server/auth/middleware/client_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ async def authenticate_request(self, request: Request) -> OAuthClientInformation

if basic_client_id != client_id:
raise AuthenticationError("Client ID mismatch in Basic auth")
except (ValueError, UnicodeDecodeError, binascii.Error):
raise AuthenticationError("Invalid Basic authentication header")
except (ValueError, UnicodeDecodeError, binascii.Error) as e:
raise AuthenticationError("Invalid Basic authentication header") from e

elif client.token_endpoint_auth_method == "client_secret_post":
raw_form_data = form_data.get("client_secret")
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/mcpserver/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ async def render(
except MCPError:
raise
except Exception as e:
raise ValueError(f"Error rendering prompt {self.name}: {e}")
raise ValueError(f"Error rendering prompt {self.name}: {e}") from e
8 changes: 4 additions & 4 deletions src/mcp/server/mcpserver/resources/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def read(self) -> str | bytes:
except MCPError:
raise
except Exception as e:
raise ValueError(f"Error reading resource {self.uri}: {e}")
raise ValueError(f"Error reading resource {self.uri}: {e}") from e

@classmethod
def from_function(
Expand Down Expand Up @@ -188,7 +188,7 @@ async def read(self) -> str | bytes:
return await anyio.to_thread.run_sync(self.path.read_bytes)
return await anyio.to_thread.run_sync(partial(self.path.read_text, encoding=self.encoding))
except Exception as e:
raise ValueError(f"Error reading file {self.path}: {e}")
raise ValueError(f"Error reading file {self.path}: {e}") from e


class HttpResource(Resource):
Expand Down Expand Up @@ -233,7 +233,7 @@ def list_files(self) -> list[Path]: # pragma: no cover
return list(self.path.glob(self.pattern)) if not self.recursive else list(self.path.rglob(self.pattern))
return list(self.path.glob("*")) if not self.recursive else list(self.path.rglob("*"))
except Exception as e:
raise ValueError(f"Error listing directory {self.path}: {e}")
raise ValueError(f"Error listing directory {self.path}: {e}") from e

async def read(self) -> str: # Always returns JSON string # pragma: no cover
"""Read the directory listing."""
Expand All @@ -242,4 +242,4 @@ async def read(self) -> str: # Always returns JSON string # pragma: no cover
file_list = [str(f.relative_to(self.path)) for f in files if f.is_file()]
return json.dumps({"files": file_list}, indent=2)
except Exception as e:
raise ValueError(f"Error reading directory {self.path}: {e}")
raise ValueError(f"Error reading directory {self.path}: {e}") from e
4 changes: 2 additions & 2 deletions src/mcp/server/mcpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ async def _handle_read_resource(
try:
results = await self.read_resource(params.uri, context)
except ResourceNotFoundError as err:
raise MCPError(code=INVALID_PARAMS, message=str(err), data={"uri": str(params.uri)})
raise MCPError(code=INVALID_PARAMS, message=str(err), data={"uri": str(params.uri)}) from err
except ResourceError as err:
raise MCPError(code=INTERNAL_ERROR, message=str(err), data={"uri": str(params.uri)})
raise MCPError(code=INTERNAL_ERROR, message=str(err), data={"uri": str(params.uri)}) from err
if isinstance(results, InputRequiredResult):
return results
contents: list[TextResourceContents | BlobResourceContents] = []
Expand Down
Loading