|
| 1 | +from contextlib import asynccontextmanager |
| 2 | +from datetime import datetime, timezone |
| 3 | +from typing import Sequence |
| 4 | + |
| 5 | +from langchain_core.messages import HumanMessage, SystemMessage |
| 6 | +from langchain_core.tools import BaseTool |
| 7 | +from pydantic import BaseModel, Field |
| 8 | +from uipath.agent.models.agent import AgentMcpResourceConfig, AgentMcpTool |
| 9 | +from uipath.agent.react import AGENT_SYSTEM_PROMPT_TEMPLATE |
| 10 | + |
| 11 | +from uipath_langchain.agent.react import create_agent |
| 12 | +from uipath_langchain.agent.tools import create_mcp_tools |
| 13 | +from uipath_langchain.chat import OpenAIModels, UiPathChat |
| 14 | + |
| 15 | +# LLM Model Configuration |
| 16 | +llm = UiPathChat( |
| 17 | + model=OpenAIModels.gpt_5_mini_2025_08_07, |
| 18 | + temperature=0.0, |
| 19 | + max_tokens=16384, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# Input/Output Models |
| 24 | +class AgentInput(BaseModel): |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class AgentOutput(BaseModel): |
| 29 | + content: str | None = Field(None, description="Output content") |
| 30 | + |
| 31 | + |
| 32 | +# Agent Messages Function |
| 33 | +def create_messages(state: AgentInput) -> Sequence[SystemMessage | HumanMessage]: |
| 34 | + # Apply system prompt template |
| 35 | + current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d") |
| 36 | + system_prompt_content = f"""You are an agentic assistant.""" |
| 37 | + enhanced_system_prompt = ( |
| 38 | + AGENT_SYSTEM_PROMPT_TEMPLATE.replace("{{systemPrompt}}", system_prompt_content) |
| 39 | + .replace("{{currentDate}}", current_date) |
| 40 | + .replace("{{agentName}}", "Mr Assistant") |
| 41 | + ) |
| 42 | + |
| 43 | + return [ |
| 44 | + SystemMessage(content=enhanced_system_prompt), |
| 45 | + HumanMessage(content=f"""Do me an RPA echo of "asdf"."""), |
| 46 | + ] |
| 47 | + |
| 48 | + |
| 49 | +mcpTools = [ |
| 50 | + AgentMcpResourceConfig( |
| 51 | + name="uipath-server", |
| 52 | + description="a", |
| 53 | + folderPath="611ca479-1f38-4abc-b2c6-6a61fa002978", |
| 54 | + slug="uipath-server", |
| 55 | + availableTools=[], |
| 56 | + isEnabled=True, |
| 57 | + ), |
| 58 | + AgentMcpResourceConfig( |
| 59 | + name="uipath-server-2", |
| 60 | + description="a", |
| 61 | + folderPath="611ca479-1f38-4abc-b2c6-6a61fa002978", |
| 62 | + slug="uipath-server-2", |
| 63 | + availableTools=[], |
| 64 | + isEnabled=True, |
| 65 | + ), |
| 66 | + AgentMcpResourceConfig( |
| 67 | + name="hello-world", |
| 68 | + description="a", |
| 69 | + folderPath="611ca479-1f38-4abc-b2c6-6a61fa002978", |
| 70 | + slug="hello-world", |
| 71 | + availableTools=[], |
| 72 | + isEnabled=True, |
| 73 | + ), |
| 74 | + AgentMcpResourceConfig( |
| 75 | + name="mcp-hello-world-24-25", |
| 76 | + description="a", |
| 77 | + folderPath="611ca479-1f38-4abc-b2c6-6a61fa002978", |
| 78 | + slug="mcp-hello-world-24-25", |
| 79 | + availableTools=[ |
| 80 | + AgentMcpTool( |
| 81 | + name="add", |
| 82 | + description=""" |
| 83 | + Add two numbers together. |
| 84 | + Args: |
| 85 | + a: First number |
| 86 | + b: Second number |
| 87 | +
|
| 88 | + Returns: |
| 89 | + Sum of a and b |
| 90 | + """, |
| 91 | + inputSchema={ |
| 92 | + "type": "object", |
| 93 | + "properties": { |
| 94 | + "a": {"type": "number", "title": "A"}, |
| 95 | + "b": {"type": "number", "title": "B"}, |
| 96 | + }, |
| 97 | + "required": ["a", "b"], |
| 98 | + }, |
| 99 | + ) |
| 100 | + ], |
| 101 | + isEnabled=True, |
| 102 | + ), |
| 103 | +] |
| 104 | + |
| 105 | +all_tools: list[BaseTool] = [] |
| 106 | + |
| 107 | + |
| 108 | +@asynccontextmanager |
| 109 | +async def make_graph(): |
| 110 | + async with create_mcp_tools(mcpTools) as tools: |
| 111 | + yield create_agent( |
| 112 | + model=llm, |
| 113 | + tools=tools + all_tools, |
| 114 | + messages=create_messages, |
| 115 | + input_schema=AgentInput, |
| 116 | + output_schema=AgentOutput, |
| 117 | + ) |
0 commit comments