Skip to content

Commit 74b0071

Browse files
author
xiaxueyan
committed
feat: mcp example
1 parent 5e61c4a commit 74b0071

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from zhipuai import ZhipuAI
2+
import zhipuai
3+
4+
import logging
5+
import logging.config
6+
7+
def test_mcp_tool_server_url_sse(logging_conf):
8+
logging.config.dictConfig(logging_conf) # type: ignore
9+
client = ZhipuAI()
10+
messages = [
11+
{"role": "user", "content": "今天是几月几号?"}
12+
]
13+
try:
14+
response = client.chat.completions.create(
15+
model="glm-4",
16+
stream=False,
17+
messages=messages,
18+
tools=[
19+
{
20+
"type": "mcp",
21+
"mcp": {
22+
"headers": {"Authorization": ""}, # 替换成用户的 header
23+
"server_label": "sougou_search",
24+
"transport_type": "sse",
25+
"server_url": "https://open.bigmodel.cn/api/mcp/sogou/sse"
26+
}
27+
},
28+
]
29+
)
30+
print(response)
31+
except Exception as err:
32+
print(err)
33+
34+
def test_mcp_tool_server_label(logging_conf):
35+
logging.config.dictConfig(logging_conf) # type: ignore
36+
client = ZhipuAI()
37+
messages = [
38+
{"role": "user", "content": "北京现在天气怎么样?"}
39+
]
40+
try:
41+
response = client.chat.completions.create(
42+
model="glm-4",
43+
stream=True,
44+
messages=messages,
45+
tools=[
46+
{
47+
"type": "mcp",
48+
"mcp": {
49+
"headers": {"Authorization": ""}, # 替换成用户的 header
50+
"server_label": "aviation",
51+
}
52+
},
53+
]
54+
)
55+
for item in response:
56+
print(item)
57+
except Exception as err:
58+
print(err)

0 commit comments

Comments
 (0)