-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathmcp_tool_creation.py
37 lines (29 loc) · 1.23 KB
/
mcp_tool_creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import asyncio
import os
from dotenv import load_dotenv
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from beeai_framework.adapters.ollama import OllamaChatModel
from beeai_framework.agents.react import ReActAgent
from beeai_framework.memory import UnconstrainedMemory
from beeai_framework.tools.mcp import MCPTool
load_dotenv()
# Create server parameters for stdio connection
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-slack"],
env={
"SLACK_BOT_TOKEN": os.environ["SLACK_BOT_TOKEN"],
"SLACK_TEAM_ID": os.environ["SLACK_TEAM_ID"],
"PATH": os.getenv("PATH", default=""),
},
)
async def slack_tool() -> MCPTool:
async with stdio_client(server_params) as (read, write), ClientSession(read, write) as session:
await session.initialize()
# Discover Slack tools via MCP client
slacktools = await MCPTool.from_client(session)
filter_tool = filter(lambda tool: tool.name == "slack_post_message", slacktools)
slack = list(filter_tool)
return slack[0]
agent = ReActAgent(llm=OllamaChatModel("llama3.1"), tools=[asyncio.run(slack_tool())], memory=UnconstrainedMemory())