Skip to content

Commit 9bc7fb6

Browse files
authored
Merge pull request #26 from UiPath/samples/multi-modal-book
samples: add simple remote mcp agent
2 parents 03f95d7 + 228efce commit 9bc7fb6

7 files changed

Lines changed: 3042 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OPENAI_API_KEY=xxx
2+
UIPATH_MCP_SERVER_URL=xxx
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
flowchart TD
2+
step__done["_done"]:::stepStyle
3+
step_process_query["process_query"]:::stepStyle
4+
event_UserQueryEvent([<p>UserQueryEvent</p>]):::defaultEventStyle
5+
event_AgentResponseEvent([<p>AgentResponseEvent</p>]):::stopEventStyle
6+
event_AgentResponseEvent --> step__done
7+
step_process_query --> event_AgentResponseEvent
8+
event_UserQueryEvent --> step_process_query
9+
classDef stepStyle fill:#f2f0ff,line-height:1.2
10+
classDef externalStyle fill:#f2f0ff,line-height:1.2
11+
classDef defaultEventStyle fill-opacity:0
12+
classDef stopEventStyle fill:#bfb6fc
13+
classDef inputRequiredStyle fill:#f2f0ff,line-height:1.2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": ["."],
3+
"workflows": {
4+
"agent": "main.py:workflow"
5+
},
6+
"env": ".env"
7+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
3+
from llama_index.core.agent.workflow import FunctionAgent
4+
from llama_index.core.workflow import (
5+
StartEvent,
6+
StopEvent,
7+
Workflow,
8+
step,
9+
)
10+
from llama_index.llms.openai import OpenAI
11+
from llama_index.tools.mcp import McpToolSpec
12+
from mcp import ClientSession
13+
from mcp.client.sse import sse_client
14+
15+
16+
# Define events
17+
class UserQueryEvent(StartEvent):
18+
"""Event representing a user query to the MCP agent."""
19+
20+
query: str
21+
22+
23+
class AgentResponseEvent(StopEvent):
24+
"""Event representing the agent's response."""
25+
26+
response: str
27+
28+
29+
# Define the workflow
30+
class MCPAgentWorkflow(Workflow):
31+
"""Workflow that uses MCP tools to respond to user queries."""
32+
33+
@step
34+
async def process_query(self, ev: UserQueryEvent) -> AgentResponseEvent:
35+
"""Process the user query using the MCP-enabled agent."""
36+
37+
# Initialize MCP client and tools
38+
async with sse_client(
39+
url=os.getenv("UIPATH_MCP_SERVER_URL"),
40+
headers={"Authorization": f"Bearer {os.getenv('UIPATH_ACCESS_TOKEN')}"},
41+
timeout=60,
42+
) as (read, write):
43+
async with ClientSession(read, write) as client_session:
44+
await client_session.initialize()
45+
mcp_tool_spec = McpToolSpec(client=client_session)
46+
tools = await mcp_tool_spec.to_tool_list_async()
47+
48+
# Initialize the agent
49+
self.agent = FunctionAgent(
50+
name="UiPath MCP Agent",
51+
description="An agent that can interact with MCP tools",
52+
llm=OpenAI(model="gpt-4o"),
53+
tools=tools,
54+
system_prompt="""You are a helpful assistant.
55+
You have access to various tools through MCP (Model Context Protocol).
56+
Use these tools to help users with their tasks.
57+
58+
Always be helpful and provide clear, actionable responses.""",
59+
)
60+
61+
response = await self.agent.run(user_msg=ev.query)
62+
63+
return AgentResponseEvent(response=str(response))
64+
65+
66+
workflow = MCPAgentWorkflow(timeout=300, verbose=True)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "llama-simple-remote-mcp-agent"
3+
version = "0.0.6"
4+
description = "UiPath LlamaIndex Remote MCP Agent"
5+
authors = [{ name = "John Doe", email = "john.doe@myemail.com" }]
6+
readme = { file = "README.md", content-type = "text/markdown" }
7+
requires-python = ">=3.10"
8+
dependencies = [
9+
"uipath-llamaindex>=0.0.22",
10+
"llama-index-llms-openai>=0.4.4",
11+
"llama-index-tools-mcp>=0.2.5"
12+
]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"entryPoints": [
3+
{
4+
"filePath": "agent",
5+
"uniqueId": "f967e1ad-bab9-4fcc-a335-e82cbe7c4ef1",
6+
"type": "agent",
7+
"input": {
8+
"type": "object",
9+
"properties": {
10+
"query": {
11+
"title": "Query",
12+
"type": "string"
13+
}
14+
},
15+
"required": [
16+
"query"
17+
]
18+
},
19+
"output": {
20+
"type": "object",
21+
"properties": {
22+
"response": {
23+
"title": "Response",
24+
"type": "string"
25+
}
26+
},
27+
"required": [
28+
"response"
29+
]
30+
}
31+
}
32+
],
33+
"bindings": {
34+
"version": "2.0",
35+
"resources": []
36+
}
37+
}

0 commit comments

Comments
 (0)