Skip to content

Commit a4bacff

Browse files
Fix: stabilize MCP client imports and unify test environment for CI & Local.
1 parent 05159af commit a4bacff

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ jobs:
3737
- name: Install dependencies
3838
run: |
3939
python -m venv venv
40-
# If mcp is private, uncomment below:
41-
# python -m pip install git+https://github.com/your-org/mcp.git
42-
./venv/bin/python -m pip install -r requirements.txt || .\venv\Scripts\python.exe -m pip install -r requirements.txt
43-
./venv/bin/python -m playwright install || .\venv\Scripts\python.exe -m playwright install
40+
if [ "${{ runner.os }}" == "Windows" ]; then
41+
.\venv\Scripts\python.exe -m pip install -r requirements.txt
42+
.\venv\Scripts\python.exe -m playwright install
43+
else
44+
./venv/bin/python -m pip install -r requirements.txt
45+
./venv/bin/python -m playwright install
46+
fi
47+
4448
4549
- name: Run tests
4650
run: |
47-
./venv/bin/python -m pytest -v --maxfail=1 --disable-warnings --html=report.html --self-contained-html \
48-
|| .\venv\Scripts\python.exe -m pytest -v --maxfail=1 --disable-warnings --html=report.html --self-contained-html
51+
if [ "${{ runner.os }}" == "Windows" ]; then
52+
.\venv\Scripts\Activate.ps1
53+
pytest -v --maxfail=1 --disable-warnings --html=report.html --self-contained-html
54+
else
55+
source venv/bin/activate
56+
pytest -v --maxfail=1 --disable-warnings --html=report.html --self-contained-html
57+
fi
58+
4959
5060
- name: Upload HTML Report
5161
uses: actions/upload-artifact@v4

playwright_mcp/mcp_client.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
# playwright_mcp/mcp_client.py
2-
3-
from mcp import client as mcp_client # import the lowercase module
1+
from mcp.client.stdio import stdio_client, StdioServerParameters
2+
from mcp.client.session import ClientSession
43
import asyncio
54

65
class MCPClient:
76
def __init__(self, server_url: str):
87
self.server_url = server_url
9-
self.client = None
8+
self.session = None
109

1110
async def connect(self):
1211
print(f"[MCP] Connecting to {self.server_url}...")
13-
self.client = mcp_client.Client(self.server_url) # use Client via mcp_client
14-
await self.client.connect()
12+
13+
# Define the server parameters
14+
server_params = StdioServerParameters(
15+
command="your_command_here", # Replace with the actual command to run your MCP server
16+
args=["your", "arguments", "here"], # Replace with any arguments needed
17+
)
18+
19+
# Use stdio_client to connect to the server
20+
async with stdio_client(server_params) as (read_stream, write_stream):
21+
self.session = ClientSession(read_stream, write_stream)
22+
await self.session.connect()
23+
1524
print("[MCP] Connected.")
1625

1726
async def send_context(self, context_data: dict):
18-
if not self.client:
19-
raise RuntimeError("MCP client not connected.")
20-
response = await self.client.query({"context": context_data})
21-
return response
27+
if self.session is None:
28+
raise RuntimeError("MCP session not connected.")
29+
result = await self.session.query({"context": context_data})
30+
return result
2231

2332
async def close(self):
24-
if self.client:
25-
await self.client.close()
33+
if self.session:
34+
await self.session.close()

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[pytest]
22
# Specify test directories
33
testpaths = tests
4+
pythonpath = .
45

56
# Add options for verbose output, HTML report generation
67
addopts = -v --maxfail=1 --disable-warnings --html=report.html --self-contained-html

report.html

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

tests/test_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from playwright_mcp.browser import launch_and_get_title
1+
# tests/test_example.py
2+
3+
from playwright_mcp.browser import launch_and_get_title
24

35
def test_example_title():
46
title = launch_and_get_title("https://example.com")

tests/test_mcp_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# tests/test_mcp_integration.py
2+
23
import asyncio
34
from playwright_mcp.mcp_adapter import run_mcp_test_flow
45

56
def test_mcp_integration():
6-
"""Ensure MCP integration and Playwright flow work end-to-end."""
7+
"""Ensure MCP and Playwright integration flow runs end-to-end."""
78
asyncio.run(run_mcp_test_flow())

0 commit comments

Comments
 (0)