Skip to content

Commit a36a4ad

Browse files
committed
feat(prompts_sample): add sample for mcp prompts
1 parent b6b23c2 commit a36a4ad

25 files changed

+5987
-0
lines changed

samples/mcp-refactoring-assistant/.agent/CLI_REFERENCE.md

Lines changed: 560 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## Required Agent Structure
2+
3+
**IMPORTANT**: All UiPath coded agents MUST follow this standard structure unless explicitly specified otherwise by the user.
4+
5+
### Required Components
6+
7+
Every agent implementation MUST include these three Pydantic models:
8+
9+
```python
10+
from pydantic import BaseModel
11+
12+
class Input(BaseModel):
13+
"""Define input fields that the agent accepts"""
14+
# Add your input fields here
15+
pass
16+
17+
class State(BaseModel):
18+
"""Define the agent's internal state that flows between nodes"""
19+
# Add your state fields here
20+
pass
21+
22+
class Output(BaseModel):
23+
"""Define output fields that the agent returns"""
24+
# Add your output fields here
25+
pass
26+
```
27+
28+
### Required LLM Initialization
29+
30+
Unless the user explicitly requests a different LLM provider, always use `UiPathChat`:
31+
32+
```python
33+
from uipath_langchain.chat import UiPathChat
34+
35+
llm = UiPathChat(model="gpt-4o-2024-08-06", temperature=0.7)
36+
```
37+
38+
**Alternative LLMs** (only use if explicitly requested):
39+
- `ChatOpenAI` from `langchain_openai`
40+
- `ChatAnthropic` from `langchain_anthropic`
41+
- Other LangChain-compatible LLMs
42+
43+
### Standard Agent Template
44+
45+
Every agent should follow this basic structure:
46+
47+
```python
48+
from langchain_core.messages import SystemMessage, HumanMessage
49+
from langgraph.graph import START, StateGraph, END
50+
from uipath_langchain.chat import UiPathChat
51+
from pydantic import BaseModel
52+
53+
# 1. Define Input, State, and Output models
54+
class Input(BaseModel):
55+
field: str
56+
57+
class State(BaseModel):
58+
field: str
59+
result: str = ""
60+
61+
class Output(BaseModel):
62+
result: str
63+
64+
# 2. Initialize UiPathChat LLM
65+
llm = UiPathChat(model="gpt-4o-2024-08-06", temperature=0.7)
66+
67+
# 3. Define agent nodes (async functions)
68+
async def process_node(state: State) -> State:
69+
response = await llm.ainvoke([HumanMessage(state.field)])
70+
return State(field=state.field, result=response.content)
71+
72+
async def output_node(state: State) -> Output:
73+
return Output(result=state.result)
74+
75+
# 4. Build the graph
76+
builder = StateGraph(State, input=Input, output=Output)
77+
builder.add_node("process", process_node)
78+
builder.add_node("output", output_node)
79+
builder.add_edge(START, "process")
80+
builder.add_edge("process", "output")
81+
builder.add_edge("output", END)
82+
83+
# 5. Compile the graph
84+
graph = builder.compile()
85+
```
86+
87+
**Key Rules**:
88+
1. Always use async/await for all node functions
89+
2. All nodes (except output) must accept and return `State`
90+
3. The final output node must return `Output`
91+
4. Use `StateGraph(State, input=Input, output=Output)` for initialization
92+
5. Always compile with `graph = builder.compile()`

samples/mcp-refactoring-assistant/.agent/SDK_REFERENCE.md

Lines changed: 619 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ANTHROPIC_API_KEY=***
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Agent Code Patterns Reference
2+
3+
This document provides practical code patterns for building UiPath coded agents using LangGraph and the UiPath Python SDK.
4+
5+
---
6+
7+
## Documentation Structure
8+
9+
This documentation is split into multiple files for efficient context loading. Load only the files you need:
10+
11+
1. **@.agent/REQUIRED_STRUCTURE.md** - Agent structure patterns and templates
12+
- **When to load:** Creating a new agent or understanding required patterns
13+
- **Contains:** Required Pydantic models (Input, State, Output), LLM initialization patterns, standard agent template
14+
15+
2. **@.agent/SDK_REFERENCE.md** - Complete SDK API reference
16+
- **When to load:** Calling UiPath SDK methods, working with services (actions, assets, jobs, etc.)
17+
- **Contains:** All SDK services and methods with full signatures and type annotations
18+
19+
3. **@.agent/CLI_REFERENCE.md** - CLI commands documentation
20+
- **When to load:** Working with `uipath init`, `uipath run`, or `uipath eval` commands
21+
- **Contains:** Command syntax, options, usage examples, and workflows
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@AGENTS.md
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Code Refactoring Assistant with MCP
2+
3+
A LangGraph agent that demonstrates how to use **MCP (Model Context Protocol)** for code analysis and refactoring guidance.
4+
5+
## What It Does
6+
7+
This agent analyzes Python code, detects issues (complexity, code smells, deep nesting), and provides tailored refactoring guidance using MCP prompts.
8+
9+
Shows how to use `client.get_prompt()` to fetch prompt templates dynamically from an MCP server.
10+
11+
12+
## MCP Components
13+
14+
### Server (`server.py`)
15+
Exposes:
16+
- **Tools**: Code analysis functions
17+
- `analyze_code_complexity` - Detects complexity metrics
18+
- `detect_code_smells` - Finds common issues
19+
- `get_refactoring_guide` - Returns which prompt to use
20+
21+
- **Prompts**: Refactoring templates
22+
- `extract_method_prompt` - For long functions
23+
- `simplify_conditional_prompt` - For nested conditions
24+
- `remove_duplication_prompt` - For duplicate code
25+
- `improve_naming_prompt` - For poor variable names
26+
27+
### Client (`graph.py`)
28+
- Agent uses tools to analyze code
29+
- Agent determines which refactoring approach to use
30+
- **`client.get_prompt()`** fetches the appropriate template from MCP server
31+
- LLM generates final refactoring guidance
32+
33+
## How to Run
34+
35+
1. **Install dependencies**:
36+
```bash
37+
uv sync
38+
```
39+
40+
2. **Initialize** (if needed):
41+
```bash
42+
uipath init
43+
```
44+
45+
3. **Run the agent**:
46+
```bash
47+
uipath run agent --input-file input.json
48+
```
49+
50+
## Example Input
51+
52+
```json
53+
{
54+
"code": "def process_data(x, y, z):\n if x:\n if y:\n if z:\n return x + y + z"
55+
}
56+
```
57+
58+
## Example Output
59+
60+
The agent will:
61+
1. Detect the issue (deep nesting)
62+
2. Fetch the `simplify_conditional_prompt` via MCP
63+
3. Generate refactoring guidance using guard clauses
64+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
flowchart TB
2+
__start__(__start__)
3+
agent(agent)
4+
prompt(prompt)
5+
__end__(__end__)
6+
__start__ --> agent
7+
agent --> prompt
8+
prompt --> __end__
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "2.0",
3+
"resources": []
4+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"$schema": "https://cloud.uipath.com/draft/2024-12/entry-point",
3+
"$id": "entry-points.json",
4+
"entryPoints": [
5+
{
6+
"filePath": "agent",
7+
"uniqueId": "426f3cb2-ac10-4969-840e-399932250e3a",
8+
"type": "agent",
9+
"input": {
10+
"type": "object",
11+
"properties": {
12+
"code": {
13+
"title": "Code",
14+
"type": "string"
15+
}
16+
},
17+
"required": [
18+
"code"
19+
]
20+
},
21+
"output": {
22+
"type": "object",
23+
"properties": {
24+
"result": {
25+
"default": null,
26+
"title": "Result",
27+
"type": "string"
28+
}
29+
},
30+
"required": []
31+
},
32+
"graph": {
33+
"nodes": [
34+
{
35+
"id": "__start__",
36+
"name": "__start__",
37+
"type": "__start__",
38+
"subgraph": null,
39+
"metadata": {}
40+
},
41+
{
42+
"id": "agent",
43+
"name": "agent",
44+
"type": "node",
45+
"subgraph": null,
46+
"metadata": {}
47+
},
48+
{
49+
"id": "prompt",
50+
"name": "prompt",
51+
"type": "node",
52+
"subgraph": null,
53+
"metadata": {}
54+
},
55+
{
56+
"id": "__end__",
57+
"name": "__end__",
58+
"type": "__end__",
59+
"subgraph": null,
60+
"metadata": {}
61+
}
62+
],
63+
"edges": [
64+
{
65+
"source": "__start__",
66+
"target": "agent",
67+
"label": null
68+
},
69+
{
70+
"source": "agent",
71+
"target": "prompt",
72+
"label": null
73+
},
74+
{
75+
"source": "prompt",
76+
"target": "__end__",
77+
"label": null
78+
}
79+
]
80+
}
81+
}
82+
]
83+
}

0 commit comments

Comments
 (0)