Skip to content

Commit 39bfc81

Browse files
committed
Addresses typing issues.
1 parent 9067b51 commit 39bfc81

File tree

8 files changed

+114
-55
lines changed

8 files changed

+114
-55
lines changed

src/cdp_context.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def my_tool_function(cdp_client):
3636
F = TypeVar("F", bound=Callable[..., Awaitable[Any]])
3737

3838

39-
def require_cdp_client(func: F) -> F:
39+
def require_cdp_client(func: F) -> Callable[..., Awaitable[Any]]:
4040
"""
4141
Decorator that provides CDP client to tool functions with automatic validation.
4242
@@ -95,7 +95,7 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
9595
except Exception as e:
9696
return create_error_response(f"CDP context error: {str(e)}")
9797

98-
return wrapper
98+
return wrapper # type: ignore[return-value]
9999

100100

101101
class CDPContext:
@@ -117,9 +117,9 @@ class CDPContext:
117117

118118
def __init__(self) -> None:
119119
"""Initialise the CDP context manager."""
120-
self.cdp_client = None
120+
self.cdp_client: Any = None
121121

122-
async def __aenter__(self):
122+
async def __aenter__(self) -> Any:
123123
"""
124124
Enter the async context and validate CDP client.
125125
@@ -145,7 +145,7 @@ async def __aenter__(self):
145145
except ImportError as e:
146146
raise RuntimeError("CDP client module not available.") from e
147147

148-
async def __aexit__(self, exc_type, exc_val, exc_tb):
148+
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
149149
"""
150150
Exit the async context.
151151
@@ -155,7 +155,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
155155
pass
156156

157157

158-
def get_cdp_client():
158+
def get_cdp_client() -> Any:
159159
"""
160160
Get the current CDP client instance without validation.
161161

src/tools/chrome_management.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@
3535
import os
3636
import platform
3737
import subprocess
38-
from typing import Any
38+
from typing import TYPE_CHECKING, Any
3939

4040
import aiohttp
4141
from mcp.server.fastmcp import FastMCP
4242

4343
from ..cdp_context import require_cdp_client
4444
from .utils import create_error_response, create_success_response
4545

46+
if TYPE_CHECKING:
47+
from ..main import ChromeDevToolsClient
48+
4649

4750
def get_chrome_executable_path(custom_path: str | None = None) -> str | None:
4851
"""Locate Chrome executable path with cross-platform detection.
@@ -446,7 +449,7 @@ async def connect_to_browser(port: int = 9222) -> dict[str, Any]:
446449

447450
@mcp.tool()
448451
@require_cdp_client
449-
async def navigate_to_url(cdp_client, url: str) -> dict[str, Any]:
452+
async def navigate_to_url(cdp_client: ChromeDevToolsClient, url: str) -> dict[str, Any]:
450453
"""Navigate the connected browser to a specific URL.
451454
452455
Instructs the currently connected Chrome instance to navigate to the specified

src/tools/console.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
from __future__ import annotations
4040

4141
import asyncio
42-
from typing import Any
42+
from typing import TYPE_CHECKING, Any
4343

4444
from mcp.server.fastmcp import FastMCP
4545

4646
from ..cdp_context import require_cdp_client
4747
from .utils import create_error_response, create_success_response, safe_timestamp_conversion
4848

49+
if TYPE_CHECKING:
50+
from ..main import ChromeDevToolsClient
51+
4952

5053
def register_console_tools(mcp: FastMCP) -> None:
5154
"""Register comprehensive console interaction tools with the MCP server.
@@ -82,7 +85,7 @@ def register_console_tools(mcp: FastMCP) -> None:
8285
@mcp.tool()
8386
@require_cdp_client
8487
async def get_console_logs(
85-
cdp_client, level: str | None = None, limit: int | None = None
88+
cdp_client: ChromeDevToolsClient, level: str | None = None, limit: int | None = None
8689
) -> dict[str, Any]:
8790
"""
8891
Get browser console logs with optional filtering.
@@ -122,7 +125,7 @@ async def get_console_logs(
122125

123126
@mcp.tool()
124127
@require_cdp_client
125-
async def get_console_error_summary(cdp_client) -> dict[str, Any]:
128+
async def get_console_error_summary(cdp_client: ChromeDevToolsClient) -> dict[str, Any]:
126129
"""
127130
Get an organised summary of console errors and warnings.
128131
@@ -172,7 +175,7 @@ async def get_console_error_summary(cdp_client) -> dict[str, Any]:
172175

173176
@mcp.tool()
174177
@require_cdp_client
175-
async def execute_javascript(cdp_client, code: str) -> dict[str, Any]:
178+
async def execute_javascript(cdp_client: ChromeDevToolsClient, code: str) -> dict[str, Any]:
176179
"""
177180
Execute JavaScript code in the browser context.
178181
@@ -211,7 +214,7 @@ async def execute_javascript(cdp_client, code: str) -> dict[str, Any]:
211214

212215
@mcp.tool()
213216
@require_cdp_client
214-
async def clear_console(cdp_client) -> dict[str, Any]:
217+
async def clear_console(cdp_client: ChromeDevToolsClient) -> dict[str, Any]:
215218
"""
216219
Clear the browser console.
217220
@@ -232,7 +235,9 @@ async def clear_console(cdp_client) -> dict[str, Any]:
232235

233236
@mcp.tool()
234237
@require_cdp_client
235-
async def inspect_console_object(cdp_client, expression: str) -> dict[str, Any]:
238+
async def inspect_console_object(
239+
cdp_client: ChromeDevToolsClient, expression: str
240+
) -> dict[str, Any]:
236241
"""
237242
Inspect a JavaScript object or expression in detail.
238243
@@ -295,7 +300,9 @@ async def inspect_console_object(cdp_client, expression: str) -> dict[str, Any]:
295300

296301
@mcp.tool()
297302
@require_cdp_client
298-
async def monitor_console_live(cdp_client, duration_seconds: int = 10) -> dict[str, Any]:
303+
async def monitor_console_live(
304+
cdp_client: ChromeDevToolsClient, duration_seconds: int = 10
305+
) -> dict[str, Any]:
299306
"""
300307
Monitor console output in real-time for a specified duration.
301308

src/tools/css.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@
4343

4444
from __future__ import annotations
4545

46-
from typing import Any
46+
from typing import TYPE_CHECKING, Any
4747

4848
from mcp.server.fastmcp import FastMCP
4949

5050
from ..cdp_context import require_cdp_client
5151
from .utils import create_error_response, create_success_response
5252

53+
if TYPE_CHECKING:
54+
from ..main import ChromeDevToolsClient
55+
5356

5457
def register_css_tools(mcp: FastMCP) -> None:
5558
"""Register comprehensive CSS analysis tools with the MCP server.
@@ -90,7 +93,7 @@ def register_css_tools(mcp: FastMCP) -> None:
9093

9194
@mcp.tool()
9295
@require_cdp_client
93-
async def get_computed_styles(cdp_client, node_id: int) -> dict[str, Any]:
96+
async def get_computed_styles(cdp_client: ChromeDevToolsClient, node_id: int) -> dict[str, Any]:
9497
"""Retrieve computed CSS styles for a DOM element.
9598
9699
Calculates and returns all computed CSS properties for the specified DOM element.
@@ -136,7 +139,7 @@ async def get_computed_styles(cdp_client, node_id: int) -> dict[str, Any]:
136139

137140
@mcp.tool()
138141
@require_cdp_client
139-
async def get_inline_styles(cdp_client, node_id: int) -> dict[str, Any]:
142+
async def get_inline_styles(cdp_client: ChromeDevToolsClient, node_id: int) -> dict[str, Any]:
140143
"""
141144
Get inline CSS styles for a DOM element.
142145
@@ -178,7 +181,7 @@ async def get_inline_styles(cdp_client, node_id: int) -> dict[str, Any]:
178181

179182
@mcp.tool()
180183
@require_cdp_client
181-
async def get_matched_styles(cdp_client, node_id: int) -> dict[str, Any]:
184+
async def get_matched_styles(cdp_client: ChromeDevToolsClient, node_id: int) -> dict[str, Any]:
182185
"""
183186
Get comprehensive style information including all CSS rules matching a DOM element.
184187
@@ -261,7 +264,9 @@ async def get_matched_styles(cdp_client, node_id: int) -> dict[str, Any]:
261264

262265
@mcp.tool()
263266
@require_cdp_client
264-
async def get_stylesheet_text(cdp_client, stylesheet_id: str) -> dict[str, Any]:
267+
async def get_stylesheet_text(
268+
cdp_client: ChromeDevToolsClient, stylesheet_id: str
269+
) -> dict[str, Any]:
265270
"""
266271
Get the textual content of a CSS stylesheet.
267272
@@ -292,7 +297,9 @@ async def get_stylesheet_text(cdp_client, stylesheet_id: str) -> dict[str, Any]:
292297

293298
@mcp.tool()
294299
@require_cdp_client
295-
async def get_background_colors(cdp_client, node_id: int) -> dict[str, Any]:
300+
async def get_background_colors(
301+
cdp_client: ChromeDevToolsClient, node_id: int
302+
) -> dict[str, Any]:
296303
"""
297304
Get background colors and font information for a DOM element.
298305
@@ -321,7 +328,7 @@ async def get_background_colors(cdp_client, node_id: int) -> dict[str, Any]:
321328

322329
@mcp.tool()
323330
@require_cdp_client
324-
async def get_platform_fonts(cdp_client, node_id: int) -> dict[str, Any]:
331+
async def get_platform_fonts(cdp_client: ChromeDevToolsClient, node_id: int) -> dict[str, Any]:
325332
"""
326333
Get platform font usage information for a DOM element.
327334
@@ -364,7 +371,7 @@ async def get_platform_fonts(cdp_client, node_id: int) -> dict[str, Any]:
364371

365372
@mcp.tool()
366373
@require_cdp_client
367-
async def get_media_queries(cdp_client) -> dict[str, Any]:
374+
async def get_media_queries(cdp_client: ChromeDevToolsClient) -> dict[str, Any]:
368375
"""
369376
Get all media queries parsed by the rendering engine.
370377
@@ -403,7 +410,9 @@ async def get_media_queries(cdp_client) -> dict[str, Any]:
403410

404411
@mcp.tool()
405412
@require_cdp_client
406-
async def collect_css_class_names(cdp_client, stylesheet_id: str) -> dict[str, Any]:
413+
async def collect_css_class_names(
414+
cdp_client: ChromeDevToolsClient, stylesheet_id: str
415+
) -> dict[str, Any]:
407416
"""
408417
Collect all class names from a specified stylesheet.
409418
@@ -434,7 +443,7 @@ async def collect_css_class_names(cdp_client, stylesheet_id: str) -> dict[str, A
434443

435444
@mcp.tool()
436445
@require_cdp_client
437-
async def start_css_coverage_tracking(cdp_client) -> dict[str, Any]:
446+
async def start_css_coverage_tracking(cdp_client: ChromeDevToolsClient) -> dict[str, Any]:
438447
"""
439448
Start tracking CSS rule usage for coverage analysis.
440449
@@ -453,7 +462,7 @@ async def start_css_coverage_tracking(cdp_client) -> dict[str, Any]:
453462

454463
@mcp.tool()
455464
@require_cdp_client
456-
async def stop_css_coverage_tracking(cdp_client) -> dict[str, Any]:
465+
async def stop_css_coverage_tracking(cdp_client: ChromeDevToolsClient) -> dict[str, Any]:
457466
"""
458467
Stop tracking CSS rule usage and get coverage results.
459468

src/tools/dom.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@
4444

4545
from __future__ import annotations
4646

47-
from typing import Any
47+
from typing import TYPE_CHECKING, Any
4848

4949
from mcp.server.fastmcp import FastMCP
5050

5151
from ..cdp_context import require_cdp_client
5252
from .utils import create_error_response, create_success_response
5353

54+
if TYPE_CHECKING:
55+
from ..main import ChromeDevToolsClient
56+
5457

5558
def register_dom_tools(mcp: FastMCP) -> None:
5659
"""Register comprehensive DOM inspection and manipulation tools with the MCP server.
@@ -91,7 +94,9 @@ def register_dom_tools(mcp: FastMCP) -> None:
9194

9295
@mcp.tool()
9396
@require_cdp_client
94-
async def get_document(cdp_client, depth: int = 1, pierce: bool = False) -> dict[str, Any]:
97+
async def get_document(
98+
cdp_client: ChromeDevToolsClient, depth: int = 1, pierce: bool = False
99+
) -> dict[str, Any]:
95100
"""Retrieve the DOM document structure with configurable depth and shadow DOM access.
96101
97102
Fetches the document tree starting from the root element, with control over
@@ -135,7 +140,9 @@ async def get_document(cdp_client, depth: int = 1, pierce: bool = False) -> dict
135140

136141
@mcp.tool()
137142
@require_cdp_client
138-
async def query_selector(cdp_client, node_id: int, selector: str) -> dict[str, Any]:
143+
async def query_selector(
144+
cdp_client: ChromeDevToolsClient, node_id: int, selector: str
145+
) -> dict[str, Any]:
139146
"""
140147
Execute querySelector on a DOM node.
141148
@@ -167,7 +174,9 @@ async def query_selector(cdp_client, node_id: int, selector: str) -> dict[str, A
167174

168175
@mcp.tool()
169176
@require_cdp_client
170-
async def query_selector_all(cdp_client, node_id: int, selector: str) -> dict[str, Any]:
177+
async def query_selector_all(
178+
cdp_client: ChromeDevToolsClient, node_id: int, selector: str
179+
) -> dict[str, Any]:
171180
"""
172181
Execute querySelectorAll on a DOM node.
173182
@@ -193,7 +202,9 @@ async def query_selector_all(cdp_client, node_id: int, selector: str) -> dict[st
193202

194203
@mcp.tool()
195204
@require_cdp_client
196-
async def get_element_attributes(cdp_client, node_id: int) -> dict[str, Any]:
205+
async def get_element_attributes(
206+
cdp_client: ChromeDevToolsClient, node_id: int
207+
) -> dict[str, Any]:
197208
"""
198209
Get all attributes of a DOM element.
199210
@@ -222,7 +233,9 @@ async def get_element_attributes(cdp_client, node_id: int) -> dict[str, Any]:
222233

223234
@mcp.tool()
224235
@require_cdp_client
225-
async def get_element_outer_html(cdp_client, node_id: int) -> dict[str, Any]:
236+
async def get_element_outer_html(
237+
cdp_client: ChromeDevToolsClient, node_id: int
238+
) -> dict[str, Any]:
226239
"""
227240
Get the outer HTML of a DOM element.
228241
@@ -249,7 +262,9 @@ async def get_element_outer_html(cdp_client, node_id: int) -> dict[str, Any]:
249262

250263
@mcp.tool()
251264
@require_cdp_client
252-
async def get_element_box_model(cdp_client, node_id: int) -> dict[str, Any]:
265+
async def get_element_box_model(
266+
cdp_client: ChromeDevToolsClient, node_id: int
267+
) -> dict[str, Any]:
253268
"""
254269
Get the box model (layout information) of a DOM element.
255270
@@ -281,7 +296,9 @@ async def get_element_box_model(cdp_client, node_id: int) -> dict[str, Any]:
281296

282297
@mcp.tool()
283298
@require_cdp_client
284-
async def describe_element(cdp_client, node_id: int, depth: int = 1) -> dict[str, Any]:
299+
async def describe_element(
300+
cdp_client: ChromeDevToolsClient, node_id: int, depth: int = 1
301+
) -> dict[str, Any]:
285302
"""
286303
Get detailed information about a DOM element.
287304
@@ -317,7 +334,9 @@ async def describe_element(cdp_client, node_id: int, depth: int = 1) -> dict[str
317334

318335
@mcp.tool()
319336
@require_cdp_client
320-
async def get_element_at_position(cdp_client, x: int, y: int) -> dict[str, Any]:
337+
async def get_element_at_position(
338+
cdp_client: ChromeDevToolsClient, x: int, y: int
339+
) -> dict[str, Any]:
321340
"""
322341
Get the DOM element at a specific screen position.
323342
@@ -346,7 +365,7 @@ async def get_element_at_position(cdp_client, x: int, y: int) -> dict[str, Any]:
346365

347366
@mcp.tool()
348367
@require_cdp_client
349-
async def search_elements(cdp_client, query: str) -> dict[str, Any]:
368+
async def search_elements(cdp_client: ChromeDevToolsClient, query: str) -> dict[str, Any]:
350369
"""
351370
Search for DOM elements matching a query string.
352371
@@ -393,7 +412,7 @@ async def search_elements(cdp_client, query: str) -> dict[str, Any]:
393412

394413
@mcp.tool()
395414
@require_cdp_client
396-
async def focus_element(cdp_client, node_id: int) -> dict[str, Any]:
415+
async def focus_element(cdp_client: ChromeDevToolsClient, node_id: int) -> dict[str, Any]:
397416
"""
398417
Focus a DOM element.
399418

0 commit comments

Comments
 (0)