1- # Generic RAG Agent with Human-in-the-Loop
1+ # Generic RAG Agent
22
3- A generic RAG (Retrieval Augmented Generation) agent that combines multiple knowledge bases with web search capabilities and optional human-in-the-loop approval for query processing .
3+ A flexible RAG (Retrieval Augmented Generation) agent that combines multiple knowledge bases with web search capabilities and MCP (Model Context Protocol) server integration .
44
55## Features
66
77- ** Multiple Knowledge Base Support** : Query multiple context grounding indexes simultaneously
88- ** Web Search Integration** : Real-time information retrieval via Tavily API
9+ - ** MCP Server Integration** : Connect to external MCP servers to access additional tools dynamically
910- ** ReAct Agent** : Uses ReAct reasoning to plan and execute queries across multiple tools
10- - ** Human-in-the-Loop Approval** : Optional human approval before processing queries for enhanced control
1111- ** Structured Output** : Generates formatted reports with summary, key findings, analysis, and recommendations
1212
1313## Prerequisites
@@ -16,6 +16,7 @@ A generic RAG (Retrieval Augmented Generation) agent that combines multiple know
1616- UiPath Orchestrator account with Context Grounding service access
1717- OpenAI API key (configured in UiPath)
1818- (Optional) Tavily API key for web search functionality
19+ - (Optional) MCP server URL for accessing additional tools
1920
2021## Configuration
2122
@@ -30,10 +31,22 @@ UIPATH_URL=https://cloud.uipath.com/your_account/your_tenant
3031UIPATH_TENANT_ID=your_tenant_id
3132UIPATH_ORGANIZATION_ID=your_organization_id
3233
33- # Optional - API Keys (if using Tavily web search)
34+ # Optional - Web Search
3435TAVILY_API_KEY=your_tavily_api_key
36+
37+ # Optional - MCP Server Integration
38+ UIPATH_MCP_URL=https://your-mcp-server-url
3539```
3640
41+ ** MCP Server Integration:**
42+ When ` UIPATH_MCP_URL ` is configured, the agent will:
43+ 1 . Connect to the MCP server at startup
44+ 2 . Retrieve all available tools from the server
45+ 3 . Add them to the agent's toolset alongside knowledge base and web search tools
46+ 4 . Keep the MCP session alive during agent execution so tools can make remote calls
47+
48+ The agent gracefully handles MCP connection failures and continues with base tools only.
49+
3750### Knowledge Base Configuration
3851
3952The agent supports multiple knowledge bases configured in [ main.py] ( main.py:21-34 ) :
@@ -80,15 +93,13 @@ The agent accepts the following input parameters in `input.json`:
8093``` json
8194{
8295 "query" : " Your question here" ,
83- "add_data_to_index" : false ,
84- "include_hitl" : false
96+ "add_data_to_index" : false
8597}
8698```
8799
88100** Parameters:**
89101- ` query ` (string): The question or query to process
90102- ` add_data_to_index ` (boolean): Whether to index data before querying (default: false)
91- - ` include_hitl ` (boolean): Whether to require human approval before processing the query (default: false)
92103
93104### Running the Agent
94105
@@ -101,8 +112,7 @@ Query existing knowledge bases without re-indexing:
101112``` json
102113{
103114 "query" : " What is our remote work policy?" ,
104- "add_data_to_index" : false ,
105- "include_hitl" : false
115+ "add_data_to_index" : false
106116}
107117```
108118
@@ -113,74 +123,76 @@ Add data to indexes before querying:
113123``` json
114124{
115125 "query" : " What is our remote work policy?" ,
116- "add_data_to_index" : true ,
117- "include_hitl" : false
126+ "add_data_to_index" : true
118127}
119128```
120129
121- #### 3. Human-in-the-Loop Mode
130+ #### 3. Multi-Tool Query Mode
122131
123- Require human approval before processing the query :
132+ Ask questions that leverage multiple tools (knowledge bases, web search, and MCP tools) :
124133
125134``` json
126135{
127- "query" : " What is our remote work policy?" ,
128- "add_data_to_index" : false ,
129- "include_hitl" : true
136+ "query" : " What is the company policy on travel spending? What is the price of GPUs in 2025? What is 5+6+7+8+(6*10)?" ,
137+ "add_data_to_index" : false
130138}
131139```
132140
133- When ` include_hitl ` is set to ` true ` , the agent will:
134-
135- 1 . ** Request Approval** : Display the query and ask for human approval:
136- ```
137- Query to be processed: "What is our remote work policy?"
138-
139- Is it okay to proceed with this query? (yes/no)
140- ```
141- 2 . ** Proceed on Approval** : If "yes", continue with query processing
142- 3 . ** Cancel on Rejection** : If "no" (or any other response), stop and return: "Query processing cancelled by user."
141+ This query will cause the agent to:
142+ - Query the knowledge base for company policy information
143+ - Use web search (Tavily) for current GPU pricing
144+ - Use MCP math tools (if configured) for calculation
143145
144146### Workflow Steps
145147
146148The agent follows this workflow:
147149
148- 1 . ** Entry Point** ([ main.py:158-171] ( main.py#L158-L171 ) ): Receives query and routing parameters, directs to HITL approval if ` include_hitl ` is true
149- 2 . ** HITL Approval** ([ main.py:173-191] ( main.py#L173-L191 ) ): Requests human approval before proceeding (if ` include_hitl ` is true)
150- 3 . ** Data Indexing** (optional): Adds documents to Context Grounding indexes if ` add_data_to_index ` is true
151- 4 . ** Wait for Ingestion** : Ensures indexes are ready before querying
152- 5 . ** Process Query with Agent** : Uses ReAct Agent to gather information from knowledge bases and web search
153- 6 . ** Format Final Answer** : Synthesizes and formats the response into a structured report
150+ 1 . ** Entry Point** : Receives query and routing parameters
151+ 2 . ** Data Indexing** (optional): Adds documents to Context Grounding indexes if ` add_data_to_index ` is true
152+ 3 . ** Wait for Ingestion** : Ensures indexes are ready before querying
153+ 4 . ** Process Query** :
154+ - Initializes base tools (knowledge bases and web search)
155+ - Connects to MCP server (if ` UIPATH_MCP_URL ` is configured) and retrieves MCP tools
156+ - Creates ReAct Agent with all available tools
157+ - Executes the agent to gather and synthesize information
158+ - Keeps MCP session alive during agent execution
159+ 5 . ** Format Final Answer** : Synthesizes and formats the response into a structured report
154160
155- ## Example Input
161+ ## Example Inputs
156162
163+ ### Simple Query
157164``` json
158165{
159- "query" : " What is our remote work policy and what are the latest AI coding tools available in 2025?" ,
160- "add_data_to_index" : false ,
161- "include_hitl" : true
166+ "query" : " What is our remote work policy?" ,
167+ "add_data_to_index" : false
162168}
163169```
164170
165- ## Security Considerations
166-
167- The human-in-the-loop query approval feature ensures that:
171+ ### Complex Multi-Tool Query
172+ ``` json
173+ {
174+ "query" : " What is our remote work policy and what are the latest AI coding tools available in 2025? Also calculate 15% of 2500." ,
175+ "add_data_to_index" : false
176+ }
177+ ```
168178
169- - ** Explicit Consent ** : Queries are only processed with explicit human approval when ` include_hitl ` is enabled
170- - ** Transparency ** : The exact query text is displayed before requesting approval
171- - ** User Control ** : Users can cancel query processing at any time by responding with anything other than "yes"
172- - ** Audit Trail ** : All approval responses are logged for tracking
179+ This will leverage:
180+ - ** Knowledge bases ** for company policy
181+ - ** Web search ** for current AI tool information
182+ - ** MCP tools ** (if configured) for percentage calculation
173183
174184## Customization
175185
176186### Adding Custom Tools
177187
178- Add custom tools by modifying the [ generate_context_grounding_query_engine_tools ] ( main.py:102-136 ) function :
188+ Add custom tools by modifying the ` generate_tools ` function in [ main.py ] ( main.py ) :
179189
180190``` python
181- def generate_context_grounding_query_engine_tools (
191+ def generate_tools (
182192 response_mode : ResponseMode,
183193) -> list[QueryEngineTool | FunctionTool]:
194+ # ... existing tools ...
195+
184196 # Add your custom tools here
185197 custom_tool = FunctionTool.from_defaults(
186198 fn = your_custom_function,
@@ -192,21 +204,32 @@ def generate_context_grounding_query_engine_tools(
192204 return tools
193205```
194206
207+ ### Connecting to Different MCP Servers
208+
209+ Simply update the ` UIPATH_MCP_URL ` in your ` .env ` file to point to a different MCP server. The agent will automatically connect and load all available tools from that server.
210+
195211### Modifying the System Prompts
196212
197- Update the agent behavior by modifying the system prompts in:
198- - ReAct Agent: [ main.py:304-310] ( main.py#L304-L310 )
199- - Final Answer Formatting: [ main.py:329-352] ( main.py#L329-L352 )
213+ Update the agent behavior by modifying the system prompts in the ` process_query ` step in [ main.py] ( main.py )
200214
201215### Adjusting Knowledge Bases
202216
203- Modify the ` INDEX_CONFIGS ` dictionary in [ main.py:21-34 ] ( main.py#L21-L34 ) to add, remove, or modify knowledge bases.
217+ Modify the ` INDEX_CONFIGS ` dictionary in [ main.py] ( main.py ) to add, remove, or modify knowledge bases.
204218
205219## Troubleshooting
206220
221+ ### MCP Connection Issues
222+
223+ If MCP tools are not loading:
224+ - Verify ` UIPATH_MCP_URL ` is correctly set in your ` .env ` file
225+ - Check that your ` UIPATH_ACCESS_TOKEN ` is valid and has not expired
226+ - Ensure the MCP server is running and accessible
227+ - Review the console output for MCP connection error messages
228+ - The agent will continue with base tools only if MCP connection fails
229+
207230### Index Ingestion Timeout
208231
209- - Increase the retry count (line 254) or wait time (line 255) in the ` wait_for_index_ingestion ` step
232+ - Increase the retry count or wait time in the ` wait_for_index_ingestion ` step
210233- Check Context Grounding service status in UiPath Orchestrator
211234- Verify your documents are properly formatted
212235
@@ -216,6 +239,13 @@ Modify the `INDEX_CONFIGS` dictionary in [main.py:21-34](main.py#L21-L34) to add
216239- Check that the Tavily API key has sufficient credits
217240- Review error messages in the agent logs
218241
242+ ### Agent Not Using MCP Tools
243+
244+ The agent uses the ReAct reasoning pattern and decides which tools to use based on the query. If MCP tools aren't being used:
245+ - Make sure your query requires functionality that the MCP tools provide
246+ - Verify that MCP tool descriptions are clear and relevant
247+ - Check the agent's reasoning output (verbose mode) to see why tools were or weren't selected
248+
219249### Missing Dependencies
220250
221251Run the installation command again:
@@ -225,9 +255,5 @@ uv sync
225255
226256## Related Samples
227257
228- - [ simple-hitl-agent] ( ../simple-hitl-agent/ ) : Basic HITL implementation with tool-level approval
229- - [ action-center-hitl-agent] ( ../action-center-hitl-agent/ ) : HITL with UiPath Action Center integration
230-
231- ## License
232-
233- This sample is provided as-is for demonstration purposes.
258+ - [ simple-remote-mcp-agent] ( ../simple-remote-mcp-agent/ ) : Simple MCP server integration example
259+ - [ context-grounding-retriever-agent] ( ../context-grounding-retriever-agent/ ) : Basic context grounding implementation
0 commit comments