Skip to content

Extract _first_json_element helper in mcp/auth.py#216

Merged
miguelgfierro merged 1 commit into
mainfrom
refactor-auth-json-helper
May 25, 2026
Merged

Extract _first_json_element helper in mcp/auth.py#216
miguelgfierro merged 1 commit into
mainfrom
refactor-auth-json-helper

Conversation

@miguelgfierro
Copy link
Copy Markdown
Contributor

Summary

_extract_tool_name, _extract_method and _extract_corpus_id on OAuthJWTMiddleware all shared the same JSON-body preamble: empty-body check, json.loads, and list-unwrap (taking the first element if the decoded value is a list). Extracted that preamble into a single static helper, _first_json_element().

Behavior preservation

  • Same exception class caught: only json.JSONDecodeError (no broadening to ValueError or Exception).
  • Same edge cases: empty body returns None; non-dict / non-list decoded payloads fall through unchanged; list payloads still unwrap to their first element.
  • Same return values across all three extractors.
  • Net diff: -1 line.

Tests

Full suite green: 1763 passed.

_extract_tool_name, _extract_method and _extract_corpus_id all shared the same JSON-body preamble (empty check, decode, list-unwrap). Pulled it into a shared static helper. Behavior-preserving: same exception class caught (JSONDecodeError only), same edge cases, same return values.
@miguelgfierro miguelgfierro merged commit c8daee1 into main May 25, 2026
9 checks passed
@miguelgfierro miguelgfierro deleted the refactor-auth-json-helper branch May 25, 2026 08:11
ancongui pushed a commit that referenced this pull request May 31, 2026
_extract_tool_name, _extract_method and _extract_corpus_id all shared the same JSON-body preamble (empty check, decode, list-unwrap). Pulled it into a shared static helper. Behavior-preserving: same exception class caught (JSONDecodeError only), same edge cases, same return values.

Co-authored-by: miguelgfierro <miguelgfierro@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant