-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.py
More file actions
71 lines (57 loc) · 2.03 KB
/
solution.py
File metadata and controls
71 lines (57 loc) · 2.03 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.agents import Agent
from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo, MCPToolset
## Legacy GitHub MCP Server
github_mcp_server = StdioServerInfo(
command="npx",
args=["-y", "@modelcontextprotocol/server-github"],
env={
"GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR GITHUB TOKEN>"
})
## Official GitHub MCP Server
github_mcp_server = StdioServerInfo(
command="docker",
args=[
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server"
],
env={
"GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR GITHUB TOKEN>"
})
print("MCP server is created")
## Add tools manually
create_issue = MCPTool(
name="create_issue",
server_info=github_mcp_server,
description="Use this tool to create issues on the given github repository"
)
get_file_contents = MCPTool(
name="get_file_contents",
server_info=github_mcp_server
)
tools = [create_issue, get_file_contents]
## Alternatively, load available tools automatically with MCPToolset
## You can load all tools in this server if you don't specify `tool_names`
# tools = MCPToolset(server_info=github_mcp_server, tool_names=["create_issue", "get_file_contents"])
print("MCP tools are created")
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
tools=tools,
system_prompt="""
You are a helpful agent that uses the tools that are provided.
Split the task into smaller tasks if necessary. Don't ask for confirmation
"""
)
print("Agent created")
## Example query to test your agent
user_input = "Can you find the typo in the README of <owner/repo> and open an issue about how to fix it?"
response = agent.run(messages=[ChatMessage.from_user(text=user_input)])
## Print the agent thinking process
print(response)
## Print the final response
print(response["messages"][-1].text)