agentscope-ai/agentscope · error · ValueError
Either client_gen or session must be provided, but not both.
Error message
Either client_gen or session must be provided, but not both.
What it means
This adapter class (MCP-related, in tool/_adapters.py) requires exactly one of client_gen or session in __init__; providing neither or both raises ValueError.
Source
Thrown at src/agentscope/tool/_adapters.py:283
self.is_read_only = False
if tool.annotations and hasattr(tool.annotations, "readOnlyHint"):
self.is_read_only = tool.annotations.readOnlyHint or False
# Store MCP tool and connection info
self._tool = tool
self._client_gen = client_gen
self._session = session
if timeout:
self._timeout = timedelta(seconds=timeout)
else:
self._timeout = None
# Validate that either client_gen or session is provided
if (client_gen is None and session is None) or (
client_gen is not None and session is not None
):
raise ValueError(
"Either client_gen or session must be provided, but not both.",
)
async def check_permissions(
self,
*_args: Any,
**_kwargs: Any,
) -> PermissionDecision:
"""Check permissions for the MCP tool usage.
Default implementation allows all operations.
Returns:
`PermissionDecision`:
Permission decision (default: ask for confirmation).
"""
if self.is_read_only:
return PermissionDecision(View on GitHub (pinned to e90f1c7592)
Solutions
- Pass only client_gen (an async factory producing an async-exit-stack pair) for lazily managed connections
- Or pass only a pre-established session for externally managed lifetimes
- Remove the redundant argument from your constructor call
Example fix
# before adapter = Adapter(client_gen=gen, session=sess) # after adapter = Adapter(client_gen=gen)
Defensive patterns
Strategy: validation
Validate before calling
assert (client_gen is None) != (session is None)
Prevention
- Choose one lifecycle model: lazy client_gen or externally managed session
- Write a small factory wrapper so call sites can't pass both
When it happens
Trigger: Constructing the adapter with both an AsyncExitStack-managed session and a client generator, or with neither (relying on some other connection mechanism).
Common situations: Migrating MCP client setup code where a session was previously created externally, then also passing client_gen; or refactoring removed the client_gen argument entirely.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- STDIO MCP does not support ephemeral mode. Use 'shared' or '
- Invalid values for MCP {card.name!r}: {e.message}
- MCP {card.name!r} needs a value for: {', '.join(sorted(missi
- MCP {card.name!r} produced an invalid client: {e}
- Exactly one of `data` or `url` must be provided.
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/8196722d0439338d.
Report an issue: GitHub.