Skip to content

Commit 6b9221c

Browse files
Merge pull request #50 from EoghanOConnor/mcp_server_script
UPDATE: Simple Agent script
2 parents b678f5d + 573f6ff commit 6b9221c

8 files changed

+199
-98
lines changed

.env.example

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
REMOTE_BASE_URL=""
2-
REMOTE_MCP_URL=""
3-
REMOTE_VDB_PROVIDER=""
1+
ANSIBLE_MCP_SERVER_URL= ""
2+
GITHUB_MCP_SERVER_URL= ""
3+
OCP_MCP_SERVER_URL= ""
4+
CUSTOM_MCP_SERVER_URL= ""
5+
REMOTE_BASE_URL= ""
6+
REMOTE_VDB_PROVIDER= ""

mcp-servers/github/queries_github.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

mcp-servers/openshift/queries_openshift.json

Lines changed: 0 additions & 30 deletions
This file was deleted.

mcp-servers/ansible/ansible_tool_queries.json renamed to tests/queries/anisble_queries.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@
3838
"tool_call": "list_inventories"
3939
}
4040
]
41-
}
42-
41+
}

mcp-servers/custom/queries_custom.json renamed to tests/queries/custom_queries.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
"tool_call": "approve_score"
2020
}
2121
]
22-
}
22+
}

tests/queries/github_queries.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"meta": {
3+
"name": "GitHub",
4+
"description": "A collection of queries for GitHub MCP Server.",
5+
"note": "Each entry includes a user query and its corresponding tool function name. Useful for LLM tool selection evaluation."
6+
},
7+
"queries": [
8+
{
9+
"query": "Search for top 5 Python repositories related to 'llama', sorted by stars.",
10+
"tool_call": "search_repositories"
11+
},
12+
{
13+
"query": "Fetch the contents of the readme file from the main branch of the 'llama-stack-on-ocp' repository by 'redhat-et'.",
14+
"tool_call": "get_file_contents"
15+
},
16+
{
17+
"query": "List the most recent 3 commits title of the 'llama-stack-on-ocp' repository by 'redhat-et'.",
18+
"tool_call": "list_commits"
19+
},
20+
{
21+
"query": "List the current open issues in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
22+
"tool_call": "list_issues"
23+
},
24+
{
25+
"query": "Search for issue mentioning 'streamlit app', only list first 3.",
26+
"tool_call": "search_issues"
27+
},
28+
{
29+
"query": "Search for the user 'RHETbot' on GitHub and describe the user.",
30+
"tool_call": "search_user"
31+
},
32+
{
33+
"query": "Get the details of issue number 5 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
34+
"tool_call": "get_issue"
35+
},
36+
{
37+
"query": "Get the details of pull request number 18 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
38+
"tool_call": "get_pull_request"
39+
},
40+
{
41+
"query": "List the top 3 most recent pull requests in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
42+
"tool_call": "list_pull_requests"
43+
},
44+
{
45+
"query": "Get the files changed in pull request number 20 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
46+
"tool_call": "get_pull_request_files"
47+
},
48+
{
49+
"query": "Get pull request status of pr number 20 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
50+
"tool_call": "get_pull_request_status"
51+
},
52+
{
53+
"query": "Get pull request comments of pr number 18 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
54+
"tool_call": "get_pull_request_commits"
55+
},
56+
{
57+
"query": "Get the reviews of pull request number 20 in the 'llama-stack-on-ocp' repository by 'redhat-et'.",
58+
"tool_call": "get_pull_request_reviews"
59+
}
60+
]
61+
}

tests/queries/ocp_queries.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"meta": {
3+
"name": "OpenShift",
4+
"description": "A collection of queries for OpenShift MCP Server.",
5+
"model": "",
6+
"note": "Each entry includes a user query and its corresponding tool function name. Useful for LLM tool selection evaluation."
7+
},
8+
"queries": [
9+
{
10+
"query": "What pods are running in the llama-serve namespace?",
11+
"tool_call": "pods_list_in_namespace"
12+
},
13+
{
14+
"query": "Can you delete the pod example in llama-serve namespace?",
15+
"tool_call": "pods_delete"
16+
},
17+
{
18+
"query": "Can you fetch the logs of the pod example in the llama-serve namespace?",
19+
"tool_call": "pods_log"
20+
},
21+
{
22+
"query": "Create a pod with the docker.io/hello-world image and expose port 8080 in the llama-serve namespace?",
23+
"tool_call": "pods_run"
24+
},
25+
{
26+
"query": "What are the events in llama-serve namespace??",
27+
"tool_call": "events_list"
28+
}
29+
]
30+
}

tests/test_mcp_servers.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Code bellow written following examples here: https://llama-stack.readthedocs.io/en/latest/building_applications
2+
from llama_stack_client.lib.agents.agent import Agent
3+
from llama_stack_client.lib.agents.event_logger import EventLogger
4+
from llama_stack_client import LlamaStackClient
5+
import logging
6+
import os
7+
from dotenv import load_dotenv
8+
import json
9+
10+
11+
12+
13+
def test_mcp_server(file_path, REMOTE_MCP_URL, model, mcp_server_name):
14+
15+
# Open the JSON file and load its content
16+
with open(file_path, 'r') as file:
17+
data = json.load(file)
18+
19+
queries = data['queries']
20+
user_prompts = [query['query'] for query in queries]
21+
22+
logger = logging.getLogger(__name__)
23+
if not logger.hasHandlers():
24+
logger.setLevel(logging.INFO)
25+
stream_handler = logging.StreamHandler()
26+
stream_handler.setLevel(logging.INFO)
27+
formatter = logging.Formatter('%(message)s')
28+
stream_handler.setFormatter(formatter)
29+
logger.addHandler(stream_handler)
30+
31+
logger.info(f"Testing {mcp_server_name} toolgroup ")
32+
logger.info(f'Model is {model} \n \n')
33+
34+
35+
# Connect to a llama stack server
36+
base_url = os.getenv('REMOTE_BASE_URL')
37+
client = LlamaStackClient(base_url=base_url)
38+
logger.info(f"Connected to Llama Stack server @ {base_url} \n")
39+
40+
# Get tool info and register tools
41+
registered_tools = client.tools.list()
42+
registered_tools_identifiers = [t.identifier for t in registered_tools]
43+
registered_toolgroups = [t.toolgroup_id for t in registered_tools]
44+
45+
if mcp_server_name not in registered_toolgroups:
46+
# Register MCP tools
47+
client.toolgroups.register(
48+
toolgroup_id=mcp_server_name,
49+
provider_id="model-context-protocol",
50+
mcp_endpoint={"uri":REMOTE_MCP_URL},
51+
)
52+
53+
logger.info(f"""Your Server has access the the following toolgroups:
54+
{set(registered_toolgroups)}
55+
""")
56+
57+
for prompt in user_prompts:
58+
# Create simple agent with tools
59+
agent = Agent(
60+
client,
61+
model=model,
62+
instructions = """You are a helpful assistant. You have access to a number of tools.
63+
Whenever a tool is called, be sure return the Response in a friendly and helpful tone.
64+
When you are asked to search the web you must use a tool. keep answer concise
65+
""" ,
66+
tools=[mcp_server_name],
67+
tool_config={"tool_choice":"auto"},
68+
sampling_params={"max_tokens":4096}
69+
)
70+
session_id = agent.create_session(session_name="Conversation_demo")
71+
turn_response = agent.create_turn(
72+
messages=[
73+
{
74+
"role":"user",
75+
"content": prompt
76+
}
77+
],
78+
session_id=session_id
79+
)
80+
for log in EventLogger().log(turn_response):
81+
log.print()
82+
83+
84+
if __name__ == "__main__":
85+
load_dotenv()
86+
87+
ansible_mcp_server_url = os.getenv('ANSIBLE_MCP_SERVER_URL')
88+
github_mcp_server_url = os.getenv('GITHUB_MCP_SERVER_URL')
89+
ocp_mcp_server_url = os.getenv('OCP_MCP_SERVER_URL')
90+
custom_mcp_server_url = os.getenv('CUSTOM_MCP_SERVER_URL')
91+
# Call the function with the file path
92+
ansilbe_file_path = './queries/anisble_queries.json'
93+
custom_file_path = './queries/custom_queries.json'
94+
github_file_path = './queries/github_queries.json'
95+
openshift_file_path = './queries/ocp_queries.json'
96+
97+
test_mcp_server(ansilbe_file_path,ansible_mcp_server_url,"meta-llama/Llama-3.2-3B-Instruct","mcp::ansible")
98+
test_mcp_server(github_file_path,github_mcp_server_url,"ibm-granite/granite-3.2-8b-instruct","mcp::github")
99+
test_mcp_server(openshift_file_path,ocp_mcp_server_url,"ibm-granite/granite-3.2-8b-instruct","mcp::openshift")
100+
test_mcp_server(custom_file_path,custom_mcp_server_url,"meta-llama/Llama-3.2-3B-Instruct","mcp::custom_tool")

0 commit comments

Comments
 (0)