Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,36 @@
|
|
|
|
|
|
1 |
from huggingface_hub import MCPClient
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
client.add_mcp_server(
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import asyncio
|
3 |
from huggingface_hub import MCPClient
|
4 |
|
5 |
+
async def main():
|
6 |
+
# Ollama's OpenAI-compatible endpoint
|
7 |
+
client = MCPClient(
|
8 |
+
provider="openai",
|
9 |
+
base_url="http://localhost:11434/v1",
|
10 |
+
model="devstral", # or your custom Ollama model name
|
11 |
+
api_key="ollama" # any non-empty string works for Ollama
|
12 |
+
)
|
13 |
|
14 |
+
# 1) Await the coroutine
|
15 |
+
await client.add_mcp_server(
|
16 |
+
type="stdio",
|
17 |
+
command="npx",
|
18 |
+
args=["@playwright/mcp@latest"]
|
19 |
+
)
|
20 |
|
21 |
+
# 2) Iterate with 'async for' over the async generator
|
22 |
+
user_msg = {
|
23 |
+
"role": "user",
|
24 |
+
"content": "Open the browser and list the titles on https://news.ycombinator.com"
|
25 |
+
}
|
26 |
+
|
27 |
+
async for chunk in client.process_single_turn_with_tools(messages=[user_msg]):
|
28 |
+
# chunks are streamed tool/assistant updates; print or handle them
|
29 |
+
print(chunk)
|
30 |
+
|
31 |
+
# optional: close network/process resources if the client exposes it
|
32 |
+
if hasattr(client, "aclose"):
|
33 |
+
await client.aclose()
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
asyncio.run(main())
|