mcp-security icon indicating copy to clipboard operation
mcp-security copied to clipboard

Modernize codebase to Python >=3.11 syntax and typing (PEP 585, PEP 604)

Open dandye opened this issue 3 weeks ago • 2 comments

Context

Python 3.10 reached end of life on October 1, 2026 under PEP 619, with 3.10.22 as its final source-only release. All five pyproject.toml files in this repository already declare requires-python = ">=3.11", run-with-google-adk/Dockerfile builds on python:3.11-slim, and server/gti/gti_mcp/utils.py already uses Python 3.11's asyncio.TaskGroup.

Much of the codebase still mixes pre-3.9 and pre-3.10 typing aliases with newer annotations. Recent reviews such as #335 and #343 highlighted this directly in server/gti, where functions annotated with PEP 585 list[dict[str, typing.Any]] sit next to typing.Union[typing.List[typing.Dict[str, typing.Any]], typing.Dict[str, str]]. Across non-marketplace Python modules on main, there are currently 215 legacy collection generics (List, Dict, Tuple, Type) and 372 Optional or Union annotations across 50 files, plus the generated modules under server/secops-soar/secops_soar_mcp/marketplace/.

Scope

  1. Built-in generic types from PEP 585. Replace typing.List, typing.Dict, typing.Set, typing.FrozenSet, typing.Tuple, and typing.Type with built-in list[...], dict[...], set[...], frozenset[...], tuple[...], and type[...], and import abstract interfaces such as Sequence, Mapping, Iterable, and Callable from collections.abc instead of typing (Ruff rules UP006 and UP035). For example, typing.Union[typing.List[typing.Dict[str, typing.Any]], typing.Dict[str, str]] becomes list[dict[str, typing.Any]] | dict[str, str].
  2. Union syntax from PEP 604. Replace typing.Optional[T] and typing.Union[A, B] with T | None and A | B (Ruff rules UP007 and UP045).
  3. Python 3.11+ language and standard library features where they simplify code. Adopt enum.StrEnum for string enums, asyncio.timeout over asyncio.wait_for where appropriate, tomllib in tests or tooling that read pyproject.toml, and typing.Self, typing.Required, or typing.NotRequired (PEPs 655 and 673) where applicable.
  4. Package metadata consistency. Add python_requires=">=3.11" to server/gti/setup.py, server/secops/setup.py, server/secops-soar/setup.py, and server/scc/setup.py so setup.py matches pyproject.toml, and update Python version classifiers for supported 3.11+ releases.

Guardrails for MCP and ADK schemas

  • Do not add from __future__ import annotations to FastMCP or Google ADK modules. Python 3.11+ evaluates PEP 585 and PEP 604 annotations natively at runtime. Stringified annotations from PEP 563 are unnecessary on Python 3.11+ and can break runtime signature inspection in Google ADK and FastMCP.
  • Preserve nullable defaults on MCP tool parameters. Keep param: str | None = None rather than param: str = None so FastMCP emits null in inputSchema and accepts explicit null arguments, matching the regression test added in #323.
  • Keep pull requests scoped by package. Please submit separate PRs per package (server/gti, server/secops, server/scc, run-with-google-adk, server/secops-soar core modules, and server/secops-soar/secops_soar_mcp/marketplace/ separately) and run each package's pytest suite, including list_tools() and call_tool() checks, to confirm inputSchema and outputSchema generation stays intact.

dandye avatar Nov 10 '26 02:11 dandye

Hi @dandye , I'd like to work on this one.

My plan is to follow the per-package split you described and start with a single package (server/scc) so you can review the approach on a small diff before I do the larger ones:

  1. Run Ruff with target-version = "py311" and the UP006, UP007, UP035, UP045 rules for that package, then go through the changes by hand instead of applying everything blindly.
  2. Switch abstract types (Sequence, Mapping, Iterable, Callable) to collections.abc, and use StrEnum / asyncio.timeout only where it actually simplifies the code.
  3. Add python_requires=">=3.11" to the matching setup.py and update the classifiers.
  4. Follow the guardrails: no from __future__ import annotations in FastMCP/ADK modules, and keep nullable defaults as param: str | None = None.
  5. Run the package's pytest suite plus the list_tools() / call_tool() checks, and compare the generated inputSchema / outputSchema before and after so I can confirm nothing changed.

Once that first PR looks good to you, I'll move on to server/secops, server/gti, run-with-google-adk, and then the secops-soar core and marketplace modules, one PR each.

If that sounds reasonable, could you assign this issue to me? Thanks!

IMMRM avatar Nov 10 '26 11:11 IMMRM

Hi @dandye , I've opened #347 for this. It covers all six areas listed in the issue as a single PR with one commit per package, so it can be reviewed commit by commit. I'm happy to split it into separate PRs if you'd prefer.

One finding worth flagging before review: FastMCP (mcp 1.10 through 1.30) treats typing.Dict[str, X] and dict[str, X] differently on tool return annotations. Built-in dict[...] gets an unwrapped <tool>DictOutput schema, typing.Dict[...] gets the wrapped {"result": ...} schema, and bare dict gets no outputSchema at all. Converting those annotations would change outputSchema and the structuredContent shape for 62 tools (12 gti, 44 secops, 6 scc).

To keep schemas unchanged, as the guardrails ask, the PR leaves those 62 return annotations as typing.Dict with # noqa: UP006 and converts everything else. list_tools() output is byte-identical before and after for all 2,258 tools. If you'd rather accept the unwrapped schemas, I can switch the PR to that.

atrearyan avatar Nov 10 '26 18:11 atrearyan