Skip to content

Commit 385ba7b

Browse files
feat: init repo
0 parents  commit 385ba7b

File tree

165 files changed

+19527
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+19527
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*config.yaml
2+
__pycache__/
3+
.idea
4+
.DS_Store
5+
.venv/

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MIT License
2+
3+
Copyright (c) Bytedance, Inc. and affiliates.
4+
5+
Permission is hereby granted, free of charge, to any person
6+
obtaining a copy of this software and associated documentation
7+
files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use,
9+
copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following
12+
conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Model Context Protocol servers and demo agents
2+
火山引擎 agent / mcp server 共享仓库
3+
4+
## Ref
5+
### mcp client
6+
- cline: 支持方舟 豆包/deepseek
7+
### mcp protocol
8+
- [Typescript MCP SDK](https://github.com/modelcontextprotocol/typescript-sdk)
9+
- [Python MCP SDK](https://github.com/modelcontextprotocol/python-sdk)
10+
- [MCP intro](https://modelcontextprotocol.io/introduction)
11+

app/general_client/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# General MCP client
2+
3+
## Introduction
4+
5+
A basic implementation of a MCP client working with one mcp server
6+
7+
## Prerequisites
8+
9+
Python >=3.10,<3.12
10+
11+
```
12+
pip install -r requirement.txt
13+
```
14+
15+
## Quick Start
16+
17+
1. start a calculator MCP server
18+
19+
```bash
20+
ARK_API_KEY=<YOUR API KEY> ARK_TOOL_CACULATOR=true PORT=8765 uvx --from git+https://github.com/volcengine/mcp-server.git#subdirectory=server/mcp_server_ark mcp-server-ark --transport sse
21+
```
22+
23+
2. set up environment variable
24+
25+
``` bash
26+
export ARK_API_KEY=<YOUR API KEY>
27+
```
28+
29+
3. start your client
30+
31+
```bash
32+
python3 client.py
33+
```
34+
35+
4. run test query and see your output
36+
1. example query to trigger calculator: 计算12341234/2341234+1123
37+
38+
![alt text](assets/image.png)
394 KB
Loading

app/general_client/client.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from typing import Any, Optional
2+
from arkitect.core.component.context.context import Context
3+
from arkitect.core.component.context.model import State
4+
from arkitect.core.component.tool.builder import build_mcp_clients_from_config
5+
6+
from arkitect.core.component.context.hooks import (
7+
PreToolCallHook,
8+
PostToolCallHook,
9+
)
10+
11+
CONFIG_FILE_PATH = "./mcp_config.json"
12+
13+
14+
class MyHooks(PreToolCallHook, PostToolCallHook):
15+
async def pre_tool_call(
16+
self,
17+
name: str,
18+
arguments: str,
19+
state: State,
20+
) -> State:
21+
print("\n" + "=" * 20 + "Inside pre tool call" + "=" * 20 + "\n")
22+
last_assistant_message = state.messages[-1]
23+
tool_call_part = last_assistant_message["tool_calls"]
24+
for tool_call in tool_call_part:
25+
print(
26+
f"Tool {tool_call['function']['name']} with {tool_call['function']['arguments']}"
27+
)
28+
# you may modify this or ask users for approval here
29+
return state # return state no matter if have modified it
30+
31+
async def post_tool_call(
32+
self,
33+
name: str,
34+
arguments: str,
35+
response: Any,
36+
exception: Optional[Exception],
37+
state: State,
38+
) -> State:
39+
print("\n" + "=" * 20 + "Inside post tool call" + "=" * 20 + "\n")
40+
print(f"Tool {name} with {arguments} returned {response}")
41+
return state # return state no matter if have modified it
42+
43+
44+
class Agent:
45+
def __init__(self, mcp_config_file: str):
46+
# Initialize session and client objects
47+
self.mcp_config_file = mcp_config_file
48+
49+
async def process_query(self, query: str) -> str:
50+
mcp_clients, cleanup = build_mcp_clients_from_config(self.mcp_config_file)
51+
messages = [
52+
{
53+
"role": "user",
54+
"content": query,
55+
}
56+
]
57+
58+
# Initialize LLM
59+
ctx = Context(
60+
model="deepseek-v3-241226",
61+
tools=list(
62+
mcp_clients.values()
63+
), # 直接在这个list里传入你的所有的python方法或者MCPClient,可以混着传入
64+
)
65+
my_hook = MyHooks()
66+
ctx.set_pre_tool_call_hook(my_hook)
67+
ctx.set_post_tool_call_hook(my_hook)
68+
await ctx.init()
69+
70+
completion = await ctx.completions.create(messages, stream=True)
71+
async for chunk in completion:
72+
if chunk.choices:
73+
print(chunk.choices[0].delta.content, end="")
74+
await cleanup() # 注意cleanup!!!
75+
76+
async def chat_loop(self):
77+
"""Run an interactive chat loop"""
78+
print("\nMCP Client Started!")
79+
print("Type your queries or 'quit' to exit.")
80+
81+
while True:
82+
try:
83+
query = input("\nQuery: ").strip()
84+
85+
if query.lower() == "quit":
86+
break
87+
88+
await self.process_query(query)
89+
90+
except Exception as e:
91+
print(f"\nError: {str(e)}")
92+
93+
94+
async def main():
95+
agent = Agent(mcp_config_file=CONFIG_FILE_PATH)
96+
await agent.chat_loop()
97+
98+
99+
if __name__ == "__main__":
100+
import asyncio
101+
102+
asyncio.run(main())

app/general_client/mcp_config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mcpServers": {
3+
"calculator": {
4+
"url": "http://localhost:8765/sse"
5+
}
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git+https://github.com/luminghao-bytedance/ai-app-lab.git@feat/deepresearch_agent

0 commit comments

Comments
 (0)