chore: make ruff format and ty clean
Format two mcp modules with the current ruff, and narrow the text-block check in conversation_store through a typed helper so ty stops flagging dict.get on an unnarrowed block.
This commit is contained in:
@@ -539,6 +539,13 @@ def _splice_assistant_group(
|
|||||||
return _splice_by_rebuild(stored_group=stored_group, incoming=incoming)
|
return _splice_by_rebuild(stored_group=stored_group, incoming=incoming)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_text_block(blk: object) -> bool:
|
||||||
|
if not isinstance(blk, dict):
|
||||||
|
return False
|
||||||
|
block = cast("dict[str, Any]", blk)
|
||||||
|
return block.get("type") == "text" and bool(str(block.get("text", "")).strip())
|
||||||
|
|
||||||
|
|
||||||
def _splice_in_place(
|
def _splice_in_place(
|
||||||
*, stored_group: _StoredDisplayTurn, incoming: ParsedTurn
|
*, stored_group: _StoredDisplayTurn, incoming: ParsedTurn
|
||||||
) -> list[dict[str, Any]] | None:
|
) -> list[dict[str, Any]] | None:
|
||||||
@@ -570,11 +577,7 @@ def _splice_in_place(
|
|||||||
if msg["role"] != "assistant" or not isinstance(content, list):
|
if msg["role"] != "assistant" or not isinstance(content, list):
|
||||||
continue
|
continue
|
||||||
positions.extend(
|
positions.extend(
|
||||||
(mi, bi)
|
(mi, bi) for bi, blk in enumerate(content) if _is_text_block(blk)
|
||||||
for bi, blk in enumerate(content)
|
|
||||||
if isinstance(blk, dict)
|
|
||||||
and blk.get("type") == "text"
|
|
||||||
and str(blk.get("text", "")).strip()
|
|
||||||
)
|
)
|
||||||
if len(positions) != len(new_texts):
|
if len(positions) != len(new_texts):
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -84,23 +84,19 @@ class LenientStdioTransport(ClientTransport):
|
|||||||
self, **session_kwargs: Unpack[SessionKwargs]
|
self, **session_kwargs: Unpack[SessionKwargs]
|
||||||
) -> AsyncIterator[ClientSession]:
|
) -> AsyncIterator[ClientSession]:
|
||||||
errlog: TextIO = self.log_file if self.log_file is not None else sys.stderr
|
errlog: TextIO = self.log_file if self.log_file is not None else sys.stderr
|
||||||
async with _lenient_stdio_client(
|
async with (
|
||||||
|
_lenient_stdio_client(
|
||||||
StdioServerParameters(
|
StdioServerParameters(
|
||||||
command=self.command,
|
command=self.command, args=self.args, env=self.env, cwd=self.cwd
|
||||||
args=self.args,
|
|
||||||
env=self.env,
|
|
||||||
cwd=self.cwd,
|
|
||||||
),
|
),
|
||||||
errlog=errlog,
|
errlog=errlog,
|
||||||
) as (read_stream, write_stream), ClientSession(
|
) as (read_stream, write_stream),
|
||||||
read_stream, write_stream, **session_kwargs
|
ClientSession(read_stream, write_stream, **session_kwargs) as session,
|
||||||
) as session:
|
):
|
||||||
yield session
|
yield session
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return f"<LenientStdioTransport(command={self.command!r}, args={self.args!r})>"
|
||||||
f"<LenientStdioTransport(command={self.command!r}, args={self.args!r})>"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.asynccontextmanager
|
@contextlib.asynccontextmanager
|
||||||
@@ -133,9 +129,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s
|
|||||||
command=command,
|
command=command,
|
||||||
args=server.args,
|
args=server.args,
|
||||||
env=(
|
env=(
|
||||||
{**env_default, **server.env}
|
{**env_default, **server.env} if server.env is not None else env_default
|
||||||
if server.env is not None
|
|
||||||
else env_default
|
|
||||||
),
|
),
|
||||||
errlog=errlog,
|
errlog=errlog,
|
||||||
cwd=server.cwd,
|
cwd=server.cwd,
|
||||||
@@ -164,9 +158,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s
|
|||||||
if not stripped:
|
if not stripped:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
message = types.JSONRPCMessage.model_validate_json(
|
message = types.JSONRPCMessage.model_validate_json(stripped)
|
||||||
stripped
|
|
||||||
)
|
|
||||||
except Exception: # noqa: BLE001 — by design, see module doc
|
except Exception: # noqa: BLE001 — by design, see module doc
|
||||||
_log.debug(
|
_log.debug(
|
||||||
"lenient stdio: dropped non-JSON line: %r",
|
"lenient stdio: dropped non-JSON line: %r",
|
||||||
@@ -194,10 +186,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s
|
|||||||
except anyio.ClosedResourceError:
|
except anyio.ClosedResourceError:
|
||||||
await anyio.lowlevel.checkpoint()
|
await anyio.lowlevel.checkpoint()
|
||||||
|
|
||||||
async with (
|
async with anyio.create_task_group() as tg, process:
|
||||||
anyio.create_task_group() as tg,
|
|
||||||
process,
|
|
||||||
):
|
|
||||||
tg.start_soon(stdout_reader)
|
tg.start_soon(stdout_reader)
|
||||||
tg.start_soon(stdin_writer)
|
tg.start_soon(stdin_writer)
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -62,9 +62,7 @@ class PythonToolMcp(_BaseMcp):
|
|||||||
tools: tuple[Callable[..., object], ...]
|
tools: tuple[Callable[..., object], ...]
|
||||||
|
|
||||||
|
|
||||||
McpServerT = Annotated[
|
McpServerT = Annotated[StdioMcp | HttpMcp | PythonToolMcp, Field(discriminator="kind")]
|
||||||
StdioMcp | HttpMcp | PythonToolMcp, Field(discriminator="kind")
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class McpServer:
|
class McpServer:
|
||||||
|
|||||||
Reference in New Issue
Block a user