From 93e0ff81a72dccb4460eb2e0c56d950f31e4a091 Mon Sep 17 00:00:00 2001 From: Alexander Whitestone Date: Mon, 23 Mar 2026 14:37:38 -0400 Subject: [PATCH] refactor: break up _build_gitea_tools() into per-operation helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract each Gitea tool builder into its own top-level function: - _build_list_issues_tool(base_url, token, owner, repo) - _build_create_issue_tool(base_url, token, owner, repo) - _build_read_issue_tool(base_url, token, owner, repo) _build_gitea_tools() is now 12 lines (was 137) — just assembles the three helpers with shared connection params. Fixes #1134 Co-Authored-By: Claude Sonnet 4.6 --- src/timmy/mcp_bridge.py | 148 ++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 65 deletions(-) diff --git a/src/timmy/mcp_bridge.py b/src/timmy/mcp_bridge.py index c1caf6e3..ba9448e3 100644 --- a/src/timmy/mcp_bridge.py +++ b/src/timmy/mcp_bridge.py @@ -142,18 +142,8 @@ def _build_shell_tool() -> MCPToolDef | None: return None -def _build_gitea_tools() -> list[MCPToolDef]: - """Build Gitea MCP tool definitions for direct Ollama bridge use. - - These tools call the Gitea REST API directly via httpx rather than - spawning an MCP server subprocess, keeping the bridge lightweight. - """ - if not settings.gitea_enabled or not settings.gitea_token: - return [] - - base_url = settings.gitea_url - token = settings.gitea_token - owner, repo = settings.gitea_repo.split("/", 1) +def _build_list_issues_tool(base_url: str, token: str, owner: str, repo: str) -> MCPToolDef: + """Build the list_issues tool for a specific Gitea repo.""" async def _list_issues(**kwargs: Any) -> str: state = kwargs.get("state", "open") @@ -178,6 +168,30 @@ def _build_gitea_tools() -> list[MCPToolDef]: except Exception as exc: return f"Error listing issues: {exc}" + return MCPToolDef( + name="list_issues", + description="List issues in the Gitea repository. Returns issue numbers and titles.", + parameters={ + "type": "object", + "properties": { + "state": { + "type": "string", + "description": "Filter by state: open, closed, or all (default: open)", + }, + "limit": { + "type": "integer", + "description": "Maximum number of issues to return (default: 10)", + }, + }, + "required": [], + }, + handler=_list_issues, + ) + + +def _build_create_issue_tool(base_url: str, token: str, owner: str, repo: str) -> MCPToolDef: + """Build the create_issue tool for a specific Gitea repo.""" + async def _create_issue(**kwargs: Any) -> str: title = kwargs.get("title", "") body = kwargs.get("body", "") @@ -199,6 +213,30 @@ def _build_gitea_tools() -> list[MCPToolDef]: except Exception as exc: return f"Error creating issue: {exc}" + return MCPToolDef( + name="create_issue", + description="Create a new issue in the Gitea repository.", + parameters={ + "type": "object", + "properties": { + "title": { + "type": "string", + "description": "Issue title (required)", + }, + "body": { + "type": "string", + "description": "Issue body in markdown (optional)", + }, + }, + "required": ["title"], + }, + handler=_create_issue, + ) + + +def _build_read_issue_tool(base_url: str, token: str, owner: str, repo: str) -> MCPToolDef: + """Build the read_issue tool for a specific Gitea repo.""" + async def _read_issue(**kwargs: Any) -> str: number = kwargs.get("number") if not number: @@ -224,60 +262,40 @@ def _build_gitea_tools() -> list[MCPToolDef]: except Exception as exc: return f"Error reading issue: {exc}" + return MCPToolDef( + name="read_issue", + description="Read details of a specific issue by number.", + parameters={ + "type": "object", + "properties": { + "number": { + "type": "integer", + "description": "Issue number to read", + }, + }, + "required": ["number"], + }, + handler=_read_issue, + ) + + +def _build_gitea_tools() -> list[MCPToolDef]: + """Build Gitea MCP tool definitions for direct Ollama bridge use. + + These tools call the Gitea REST API directly via httpx rather than + spawning an MCP server subprocess, keeping the bridge lightweight. + """ + if not settings.gitea_enabled or not settings.gitea_token: + return [] + + base_url = settings.gitea_url + token = settings.gitea_token + owner, repo = settings.gitea_repo.split("/", 1) + return [ - MCPToolDef( - name="list_issues", - description="List issues in the Gitea repository. Returns issue numbers and titles.", - parameters={ - "type": "object", - "properties": { - "state": { - "type": "string", - "description": "Filter by state: open, closed, or all (default: open)", - }, - "limit": { - "type": "integer", - "description": "Maximum number of issues to return (default: 10)", - }, - }, - "required": [], - }, - handler=_list_issues, - ), - MCPToolDef( - name="create_issue", - description="Create a new issue in the Gitea repository.", - parameters={ - "type": "object", - "properties": { - "title": { - "type": "string", - "description": "Issue title (required)", - }, - "body": { - "type": "string", - "description": "Issue body in markdown (optional)", - }, - }, - "required": ["title"], - }, - handler=_create_issue, - ), - MCPToolDef( - name="read_issue", - description="Read details of a specific issue by number.", - parameters={ - "type": "object", - "properties": { - "number": { - "type": "integer", - "description": "Issue number to read", - }, - }, - "required": ["number"], - }, - handler=_read_issue, - ), + _build_list_issues_tool(base_url, token, owner, repo), + _build_create_issue_tool(base_url, token, owner, repo), + _build_read_issue_tool(base_url, token, owner, repo), ] -- 2.43.0