Merge pull request #1 from gerlaxrex/feat/init
Browse files- .gitignore +5 -188
- .pre-commit-config.yaml +21 -0
- .python-version +1 -0
- app.py +271 -0
- main.py +83 -0
- pmcp/__init__.py +0 -0
- pmcp/agents/__init__.py +0 -0
- pmcp/agents/agent_base.py +64 -0
- pmcp/agents/trello_agent.py +36 -0
- pyproject.toml +21 -0
- uv.lock +0 -0
.gitignore
CHANGED
|
@@ -1,194 +1,11 @@
|
|
| 1 |
-
#
|
| 2 |
__pycache__/
|
| 3 |
-
*.py[
|
| 4 |
-
*$py.class
|
| 5 |
-
|
| 6 |
-
# C extensions
|
| 7 |
-
*.so
|
| 8 |
-
|
| 9 |
-
# Distribution / packaging
|
| 10 |
-
.Python
|
| 11 |
build/
|
| 12 |
-
develop-eggs/
|
| 13 |
dist/
|
| 14 |
-
downloads/
|
| 15 |
-
eggs/
|
| 16 |
-
.eggs/
|
| 17 |
-
lib/
|
| 18 |
-
lib64/
|
| 19 |
-
parts/
|
| 20 |
-
sdist/
|
| 21 |
-
var/
|
| 22 |
wheels/
|
| 23 |
-
|
| 24 |
-
*.egg-info
|
| 25 |
-
.installed.cfg
|
| 26 |
-
*.egg
|
| 27 |
-
MANIFEST
|
| 28 |
-
|
| 29 |
-
# PyInstaller
|
| 30 |
-
# Usually these files are written by a python script from a template
|
| 31 |
-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
-
*.manifest
|
| 33 |
-
*.spec
|
| 34 |
-
|
| 35 |
-
# Installer logs
|
| 36 |
-
pip-log.txt
|
| 37 |
-
pip-delete-this-directory.txt
|
| 38 |
-
|
| 39 |
-
# Unit test / coverage reports
|
| 40 |
-
htmlcov/
|
| 41 |
-
.tox/
|
| 42 |
-
.nox/
|
| 43 |
-
.coverage
|
| 44 |
-
.coverage.*
|
| 45 |
-
.cache
|
| 46 |
-
nosetests.xml
|
| 47 |
-
coverage.xml
|
| 48 |
-
*.cover
|
| 49 |
-
*.py,cover
|
| 50 |
-
.hypothesis/
|
| 51 |
-
.pytest_cache/
|
| 52 |
-
cover/
|
| 53 |
-
|
| 54 |
-
# Translations
|
| 55 |
-
*.mo
|
| 56 |
-
*.pot
|
| 57 |
-
|
| 58 |
-
# Django stuff:
|
| 59 |
-
*.log
|
| 60 |
-
local_settings.py
|
| 61 |
-
db.sqlite3
|
| 62 |
-
db.sqlite3-journal
|
| 63 |
-
|
| 64 |
-
# Flask stuff:
|
| 65 |
-
instance/
|
| 66 |
-
.webassets-cache
|
| 67 |
-
|
| 68 |
-
# Scrapy stuff:
|
| 69 |
-
.scrapy
|
| 70 |
-
|
| 71 |
-
# Sphinx documentation
|
| 72 |
-
docs/_build/
|
| 73 |
-
|
| 74 |
-
# PyBuilder
|
| 75 |
-
.pybuilder/
|
| 76 |
-
target/
|
| 77 |
-
|
| 78 |
-
# Jupyter Notebook
|
| 79 |
-
.ipynb_checkpoints
|
| 80 |
-
|
| 81 |
-
# IPython
|
| 82 |
-
profile_default/
|
| 83 |
-
ipython_config.py
|
| 84 |
-
|
| 85 |
-
# pyenv
|
| 86 |
-
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
-
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
-
# .python-version
|
| 89 |
-
|
| 90 |
-
# pipenv
|
| 91 |
-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
-
# install all needed dependencies.
|
| 95 |
-
#Pipfile.lock
|
| 96 |
-
|
| 97 |
-
# UV
|
| 98 |
-
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
| 99 |
-
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
-
# commonly ignored for libraries.
|
| 101 |
-
#uv.lock
|
| 102 |
-
|
| 103 |
-
# poetry
|
| 104 |
-
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 105 |
-
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 106 |
-
# commonly ignored for libraries.
|
| 107 |
-
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 108 |
-
#poetry.lock
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 112 |
-
#pdm.lock
|
| 113 |
-
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 114 |
-
# in version control.
|
| 115 |
-
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 116 |
-
.pdm.toml
|
| 117 |
-
.pdm-python
|
| 118 |
-
.pdm-build/
|
| 119 |
-
|
| 120 |
-
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 121 |
-
__pypackages__/
|
| 122 |
-
|
| 123 |
-
# Celery stuff
|
| 124 |
-
celerybeat-schedule
|
| 125 |
-
celerybeat.pid
|
| 126 |
-
|
| 127 |
-
# SageMath parsed files
|
| 128 |
-
*.sage.py
|
| 129 |
-
|
| 130 |
-
# Environments
|
| 131 |
-
.env
|
| 132 |
.venv
|
| 133 |
-
env/
|
| 134 |
-
venv/
|
| 135 |
-
ENV/
|
| 136 |
-
env.bak/
|
| 137 |
-
venv.bak/
|
| 138 |
-
|
| 139 |
-
# Spyder project settings
|
| 140 |
-
.spyderproject
|
| 141 |
-
.spyproject
|
| 142 |
-
|
| 143 |
-
# Rope project settings
|
| 144 |
-
.ropeproject
|
| 145 |
-
|
| 146 |
-
# mkdocs documentation
|
| 147 |
-
/site
|
| 148 |
-
|
| 149 |
-
# mypy
|
| 150 |
-
.mypy_cache/
|
| 151 |
-
.dmypy.json
|
| 152 |
-
dmypy.json
|
| 153 |
-
|
| 154 |
-
# Pyre type checker
|
| 155 |
-
.pyre/
|
| 156 |
-
|
| 157 |
-
# pytype static type analyzer
|
| 158 |
-
.pytype/
|
| 159 |
-
|
| 160 |
-
# Cython debug symbols
|
| 161 |
-
cython_debug/
|
| 162 |
-
|
| 163 |
-
# PyCharm
|
| 164 |
-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 165 |
-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 166 |
-
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 167 |
-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 168 |
-
#.idea/
|
| 169 |
-
|
| 170 |
-
# Abstra
|
| 171 |
-
# Abstra is an AI-powered process automation framework.
|
| 172 |
-
# Ignore directories containing user credentials, local state, and settings.
|
| 173 |
-
# Learn more at https://abstra.io/docs
|
| 174 |
-
.abstra/
|
| 175 |
-
|
| 176 |
-
# Visual Studio Code
|
| 177 |
-
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
| 178 |
-
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
| 179 |
-
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
| 180 |
-
# you could uncomment the following to ignore the enitre vscode folder
|
| 181 |
-
# .vscode/
|
| 182 |
-
|
| 183 |
-
# Ruff stuff:
|
| 184 |
-
.ruff_cache/
|
| 185 |
-
|
| 186 |
-
# PyPI configuration file
|
| 187 |
-
.pypirc
|
| 188 |
-
|
| 189 |
-
# Cursor
|
| 190 |
-
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
| 191 |
-
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
| 192 |
-
# refer to https://docs.cursor.com/context/ignore-files
|
| 193 |
-
.cursorignore
|
| 194 |
-
.cursorindexingignore
|
|
|
|
| 1 |
+
# Python-generated files
|
| 2 |
__pycache__/
|
| 3 |
+
*.py[oc]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
build/
|
|
|
|
| 5 |
dist/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
wheels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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.pre-commit-config.yaml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# See https://pre-commit.com for more information
|
| 2 |
+
# See https://pre-commit.com/hooks.html for more hooks
|
| 3 |
+
repos:
|
| 4 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 5 |
+
rev: v3.2.0
|
| 6 |
+
hooks:
|
| 7 |
+
- id: trailing-whitespace
|
| 8 |
+
- id: end-of-file-fixer
|
| 9 |
+
- id: check-yaml
|
| 10 |
+
- id: check-added-large-files
|
| 11 |
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
| 12 |
+
# Ruff version.
|
| 13 |
+
rev: v0.11.10
|
| 14 |
+
hooks:
|
| 15 |
+
# Run the linter.
|
| 16 |
+
- id: ruff-check
|
| 17 |
+
types_or: [ python, pyi ]
|
| 18 |
+
args: [ --fix ]
|
| 19 |
+
# Run the formatter.
|
| 20 |
+
- id: ruff-format
|
| 21 |
+
types_or: [ python, pyi ]
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.12
|
app.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import asyncio
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from langchain_mcp_adapters.client import MultiServerMCPClient
|
| 8 |
+
from langchain_openai import ChatOpenAI
|
| 9 |
+
from langgraph.prebuilt import ToolNode
|
| 10 |
+
from langgraph.graph import MessagesState, END, StateGraph
|
| 11 |
+
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
|
| 12 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 13 |
+
|
| 14 |
+
SYSTEM_PROMPT = """
|
| 15 |
+
You are an assistant that can manage Trello boards and projects.
|
| 16 |
+
You will be given a set of tools to work with. Each time you decide to use a tool that modifies in any way a Trello board, you MUST ask the user if wants to proceed.
|
| 17 |
+
If the user's answer is negative, then you have to abort everything and end the conversation.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class LangGraphAgent:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self.agent_app = None
|
| 24 |
+
self.config = {"configurable": {"thread_id": f"{str(uuid.uuid4())}"}}
|
| 25 |
+
self.memory = MemorySaver()
|
| 26 |
+
|
| 27 |
+
def reset_thread(self):
|
| 28 |
+
"""Resets the conversation thread for the agent."""
|
| 29 |
+
self.config = {"configurable": {"thread_id": f"{str(uuid.uuid4())}"}}
|
| 30 |
+
print(
|
| 31 |
+
f"Chat thread reset. New Thread ID: {self.config['configurable']['thread_id']}"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
async def setup(self):
|
| 35 |
+
print("Setting up LangGraphAgent...")
|
| 36 |
+
mcp_client = MultiServerMCPClient(
|
| 37 |
+
{
|
| 38 |
+
"trello": {
|
| 39 |
+
"url": "http://localhost:8000/sse",
|
| 40 |
+
"transport": "sse",
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
tools = await mcp_client.get_tools()
|
| 46 |
+
tool_node = ToolNode(tools)
|
| 47 |
+
|
| 48 |
+
# Ensure NEBIUS_API_KEY is set
|
| 49 |
+
api_key = os.getenv("NEBIUS_API_KEY")
|
| 50 |
+
if not api_key:
|
| 51 |
+
raise ValueError("NEBIUS_API_KEY environment variable not set.")
|
| 52 |
+
|
| 53 |
+
llm_with_tools = ChatOpenAI(
|
| 54 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 55 |
+
temperature=0.0,
|
| 56 |
+
api_key=api_key,
|
| 57 |
+
base_url="https://api.studio.nebius.com/v1/",
|
| 58 |
+
)
|
| 59 |
+
llm_with_tools = llm_with_tools.bind_tools(tools)
|
| 60 |
+
|
| 61 |
+
async def call_llm(state: MessagesState):
|
| 62 |
+
response = await llm_with_tools.ainvoke(
|
| 63 |
+
state["messages"]
|
| 64 |
+
) # Use await for async invoke
|
| 65 |
+
return {"messages": [response]}
|
| 66 |
+
|
| 67 |
+
graph = StateGraph(MessagesState)
|
| 68 |
+
graph.add_node("llm", functools.partial(call_llm))
|
| 69 |
+
graph.add_node("tool", tool_node)
|
| 70 |
+
graph.set_entry_point("llm")
|
| 71 |
+
|
| 72 |
+
def should_continue(state: MessagesState):
|
| 73 |
+
last_message = state["messages"][-1]
|
| 74 |
+
return (
|
| 75 |
+
"tool"
|
| 76 |
+
if hasattr(last_message, "tool_calls") and last_message.tool_calls
|
| 77 |
+
else END
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
graph.add_conditional_edges("llm", should_continue, {"tool": "tool", END: END})
|
| 81 |
+
graph.add_edge("tool", "llm")
|
| 82 |
+
|
| 83 |
+
self.agent_app = graph.compile(checkpointer=self.memory)
|
| 84 |
+
print("LangGraphAgent setup complete.")
|
| 85 |
+
|
| 86 |
+
async def generate_responses_for_turn(
|
| 87 |
+
self, user_message_text: str, is_first_turn_in_ui: bool
|
| 88 |
+
) -> list[str]:
|
| 89 |
+
"""
|
| 90 |
+
Generates a list of bot utterances for the current turn based on user input.
|
| 91 |
+
"""
|
| 92 |
+
langgraph_input_messages = []
|
| 93 |
+
# The SYSTEM_PROMPT is added to the graph input only if it's the first UI interaction *and*
|
| 94 |
+
# the checkpointer implies it's the start of a new conversation for the thread_id.
|
| 95 |
+
# MemorySaver will handle loading history; system prompt is good for first message of a thread.
|
| 96 |
+
|
| 97 |
+
# Check current state in memory to decide if SystemMessage is truly needed
|
| 98 |
+
thread_state = await self.memory.aget(self.config) # Use aget for async
|
| 99 |
+
is_new_thread_conversation = (
|
| 100 |
+
not thread_state
|
| 101 |
+
or not thread_state.get("values")
|
| 102 |
+
or not thread_state["values"]["messages"]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if is_new_thread_conversation:
|
| 106 |
+
print("Adding System Prompt for new conversation thread.")
|
| 107 |
+
langgraph_input_messages.append(
|
| 108 |
+
SystemMessage(content=SYSTEM_PROMPT.strip())
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
langgraph_input_messages.append(HumanMessage(content=user_message_text))
|
| 112 |
+
|
| 113 |
+
bot_responses_this_turn = []
|
| 114 |
+
processed_message_ids_in_stream = set()
|
| 115 |
+
|
| 116 |
+
async for result in self.agent_app.astream(
|
| 117 |
+
{"messages": langgraph_input_messages},
|
| 118 |
+
config=self.config,
|
| 119 |
+
stream_mode="values",
|
| 120 |
+
):
|
| 121 |
+
if not result or "messages" not in result or not result["messages"]:
|
| 122 |
+
continue
|
| 123 |
+
|
| 124 |
+
latest_message_in_graph_state = result["messages"][-1]
|
| 125 |
+
|
| 126 |
+
if (
|
| 127 |
+
isinstance(latest_message_in_graph_state, AIMessage)
|
| 128 |
+
and latest_message_in_graph_state.id
|
| 129 |
+
not in processed_message_ids_in_stream
|
| 130 |
+
):
|
| 131 |
+
current_ai_msg = latest_message_in_graph_state
|
| 132 |
+
# Add message ID to set of processed messages to avoid duplication from multiple stream events
|
| 133 |
+
# for the same underlying message object.
|
| 134 |
+
# However, AIMessages can be broken into chunks if streaming content.
|
| 135 |
+
# For now, with stream_mode="values", we get full messages.
|
| 136 |
+
# The id check is crucial if the same AIMessage object instance appears multiple times in the stream values.
|
| 137 |
+
|
| 138 |
+
newly_generated_content_for_this_step = []
|
| 139 |
+
|
| 140 |
+
# 1. Handle AIMessage content
|
| 141 |
+
if current_ai_msg.content:
|
| 142 |
+
# Add content if it's new or different from the last added piece
|
| 143 |
+
if (
|
| 144 |
+
not bot_responses_this_turn
|
| 145 |
+
or bot_responses_this_turn[-1] != current_ai_msg.content
|
| 146 |
+
):
|
| 147 |
+
newly_generated_content_for_this_step.append(
|
| 148 |
+
str(current_ai_msg.content)
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# 2. Handle tool calls
|
| 152 |
+
if hasattr(current_ai_msg, "tool_calls") and current_ai_msg.tool_calls:
|
| 153 |
+
for tool_call in current_ai_msg.tool_calls:
|
| 154 |
+
# Check if this specific tool call has been processed (e.g., by ID if available, or by content)
|
| 155 |
+
# For simplicity, we assume each tool_call in a new AIMessage is distinct for now
|
| 156 |
+
call_str = f"**Tool Call:** `{tool_call['name']}`\n*Arguments:* `{tool_call['args']}`"
|
| 157 |
+
newly_generated_content_for_this_step.append(call_str)
|
| 158 |
+
|
| 159 |
+
if newly_generated_content_for_this_step:
|
| 160 |
+
bot_responses_this_turn.extend(
|
| 161 |
+
newly_generated_content_for_this_step
|
| 162 |
+
)
|
| 163 |
+
processed_message_ids_in_stream.add(
|
| 164 |
+
current_ai_msg.id
|
| 165 |
+
) # Mark this AIMessage ID as processed
|
| 166 |
+
|
| 167 |
+
# Deduplicate consecutive identical messages that might arise from streaming nuances
|
| 168 |
+
final_bot_responses = []
|
| 169 |
+
if bot_responses_this_turn:
|
| 170 |
+
final_bot_responses.append(bot_responses_this_turn[0])
|
| 171 |
+
for i in range(1, len(bot_responses_this_turn)):
|
| 172 |
+
if bot_responses_this_turn[i] != bot_responses_this_turn[i - 1]:
|
| 173 |
+
final_bot_responses.append(bot_responses_this_turn[i])
|
| 174 |
+
|
| 175 |
+
return final_bot_responses
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
agent = LangGraphAgent()
|
| 179 |
+
|
| 180 |
+
# Apply a theme
|
| 181 |
+
theme = gr.themes.Soft(
|
| 182 |
+
primary_hue="blue", secondary_hue="sky", neutral_hue="slate"
|
| 183 |
+
).set(
|
| 184 |
+
body_background_fill="linear-gradient(to right, #f0f4f8, #e6e9f0)", # Light gradient background
|
| 185 |
+
block_background_fill="white",
|
| 186 |
+
block_border_width="1px",
|
| 187 |
+
block_shadow="*shadow_drop_lg",
|
| 188 |
+
button_primary_background_fill="*primary_500",
|
| 189 |
+
button_primary_text_color="white",
|
| 190 |
+
button_secondary_background_fill="*secondary_500",
|
| 191 |
+
button_secondary_text_color="white",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Blocks(theme=theme, title="Trello AI Assistant") as demo:
|
| 195 |
+
gr.Markdown(
|
| 196 |
+
"""
|
| 197 |
+
# Trello AI Assistant
|
| 198 |
+
Manage your Trello boards and projects with AI assistance.
|
| 199 |
+
"""
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
chatbot = gr.Chatbot(
|
| 203 |
+
label="Conversation",
|
| 204 |
+
bubble_full_width=False,
|
| 205 |
+
height=600,
|
| 206 |
+
avatar_images=(
|
| 207 |
+
None,
|
| 208 |
+
"https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Brandon_Sanderson_in_2018.jpg/800px-Brandon_Sanderson_in_2018.jpg?20230101015657",
|
| 209 |
+
),
|
| 210 |
+
# (user_avatar, bot_avatar) - replace bot avatar with something better or remove
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
with gr.Row():
|
| 214 |
+
msg = gr.Textbox(
|
| 215 |
+
label="Your Message",
|
| 216 |
+
placeholder="Type your message here and press Enter to send...",
|
| 217 |
+
scale=4, # Make textbox take more space
|
| 218 |
+
autofocus=True,
|
| 219 |
+
)
|
| 220 |
+
# submit_button = gr.Button("Send", variant="primary", scale=1) # Optional: if you want an explicit send button
|
| 221 |
+
|
| 222 |
+
clear_button = gr.Button("🗑️ Clear Chat & Reset Conversation")
|
| 223 |
+
|
| 224 |
+
async def respond(user_message_text, chat_history):
|
| 225 |
+
if not user_message_text.strip():
|
| 226 |
+
return (
|
| 227 |
+
chat_history,
|
| 228 |
+
"",
|
| 229 |
+
) # Ignore empty input, return current history and clear textbox
|
| 230 |
+
|
| 231 |
+
is_first_turn = not chat_history
|
| 232 |
+
|
| 233 |
+
# Append user message to chat_history optimistically for immediate display
|
| 234 |
+
# Bot response will fill in the 'None' later or add new [None, bot_msg] rows
|
| 235 |
+
# This makes UI feel more responsive.
|
| 236 |
+
# chat_history.append([user_message_text, None]) # Temporarily removed for simpler logic below
|
| 237 |
+
|
| 238 |
+
ai_utterances = await agent.generate_responses_for_turn(
|
| 239 |
+
user_message_text, is_first_turn
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
if ai_utterances:
|
| 243 |
+
chat_history.append([user_message_text, ai_utterances[0]])
|
| 244 |
+
for i in range(1, len(ai_utterances)):
|
| 245 |
+
chat_history.append([None, ai_utterances[i]])
|
| 246 |
+
else:
|
| 247 |
+
# If agent provides no utterances (e.g. error or no action)
|
| 248 |
+
chat_history.append(
|
| 249 |
+
[user_message_text, "I don't have a response for that right now."]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
return chat_history, "" # Return updated history and clear the textbox
|
| 253 |
+
|
| 254 |
+
# Event handlers
|
| 255 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 256 |
+
|
| 257 |
+
# if submit_button: # If you add explicit send button
|
| 258 |
+
# submit_button.click(respond, [msg, chatbot], [chatbot, msg])
|
| 259 |
+
|
| 260 |
+
def clear_chat_and_reset_agent():
|
| 261 |
+
agent.reset_thread()
|
| 262 |
+
return [], "" # Clears chatbot UI and textbox
|
| 263 |
+
|
| 264 |
+
clear_button.click(clear_chat_and_reset_agent, None, [chatbot, msg], queue=False)
|
| 265 |
+
|
| 266 |
+
# Load agent setup when the app starts
|
| 267 |
+
# Using a lambda to ensure asyncio.run is called within the demo's event loop context if needed
|
| 268 |
+
demo.load(lambda: asyncio.run(agent.setup()))
|
| 269 |
+
|
| 270 |
+
if __name__ == "__main__":
|
| 271 |
+
demo.launch()
|
main.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pprint
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
+
from langchain_mcp_adapters.client import MultiServerMCPClient
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langgraph.prebuilt import ToolNode
|
| 8 |
+
from langgraph.graph import MessagesState, END, StateGraph
|
| 9 |
+
from langchain_core.messages import HumanMessage
|
| 10 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 11 |
+
|
| 12 |
+
from pmcp.agents.trello_agent import TrelloAgent
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
async def call_llm(llm_with_tools: ChatOpenAI, state: MessagesState):
|
| 16 |
+
response = llm_with_tools.invoke(state["messages"])
|
| 17 |
+
return {"messages": [response]}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
async def main():
|
| 21 |
+
mcp_client = MultiServerMCPClient(
|
| 22 |
+
{
|
| 23 |
+
"trello": {
|
| 24 |
+
"url": "http://localhost:8000/sse",
|
| 25 |
+
"transport": "sse",
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
memory = MemorySaver()
|
| 31 |
+
|
| 32 |
+
tools = await mcp_client.get_tools()
|
| 33 |
+
tool_node = ToolNode(tools)
|
| 34 |
+
llm = ChatOpenAI(
|
| 35 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 36 |
+
temperature=0.0,
|
| 37 |
+
api_key=os.getenv("NEBIUS_API_KEY"),
|
| 38 |
+
base_url="https://api.studio.nebius.com/v1/",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
trello_agent = TrelloAgent(
|
| 42 |
+
tools=tools,
|
| 43 |
+
llm=llm,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
graph = StateGraph(MessagesState)
|
| 47 |
+
|
| 48 |
+
graph.add_node("llm", trello_agent.acall_trello_agent)
|
| 49 |
+
graph.add_node("tool", tool_node)
|
| 50 |
+
graph.set_entry_point("llm")
|
| 51 |
+
|
| 52 |
+
def should_continue(state: MessagesState):
|
| 53 |
+
last_message = state["messages"][-1]
|
| 54 |
+
if last_message.tool_calls:
|
| 55 |
+
return "tool"
|
| 56 |
+
return END
|
| 57 |
+
|
| 58 |
+
graph.add_conditional_edges("llm", should_continue, {"tool": "tool", END: END})
|
| 59 |
+
graph.add_edge("tool", "llm")
|
| 60 |
+
app = graph.compile(checkpointer=memory)
|
| 61 |
+
|
| 62 |
+
user_input = input("user >")
|
| 63 |
+
config = {"configurable": {"thread_id": f"{str(uuid.uuid4())}"}}
|
| 64 |
+
|
| 65 |
+
while user_input.lower() != "q":
|
| 66 |
+
async for res in app.astream(
|
| 67 |
+
{
|
| 68 |
+
"messages": [
|
| 69 |
+
HumanMessage(content=user_input),
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
config=config,
|
| 73 |
+
stream_mode="values",
|
| 74 |
+
):
|
| 75 |
+
pprint.pprint(res["messages"][-1])
|
| 76 |
+
pprint.pprint("-------------------------------------")
|
| 77 |
+
user_input = input("user >")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
import asyncio
|
| 82 |
+
|
| 83 |
+
asyncio.run(main())
|
pmcp/__init__.py
ADDED
|
File without changes
|
pmcp/agents/__init__.py
ADDED
|
File without changes
|
pmcp/agents/agent_base.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import List, TypeVar, Optional
|
| 3 |
+
|
| 4 |
+
from langchain_core.messages import AnyMessage, BaseMessage
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from langchain_core.tools import BaseTool
|
| 7 |
+
from langchain_openai import ChatOpenAI
|
| 8 |
+
|
| 9 |
+
OutStruct = TypeVar(name="OutStruct", bound=BaseModel)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class AgentBlueprint:
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
agent_name: str,
|
| 16 |
+
*,
|
| 17 |
+
system_prompt: str = "",
|
| 18 |
+
tools: List[BaseTool] = None,
|
| 19 |
+
description: str = "",
|
| 20 |
+
base_url: Optional[str] = None,
|
| 21 |
+
llm: ChatOpenAI,
|
| 22 |
+
):
|
| 23 |
+
self.agent_name = agent_name
|
| 24 |
+
self.system_prompt = system_prompt
|
| 25 |
+
self.llm = llm
|
| 26 |
+
self.agent_description = description
|
| 27 |
+
if tools:
|
| 28 |
+
self.llm = self.llm.bind_tools(tools)
|
| 29 |
+
self.tools = {tool.name: tool for tool in tools}
|
| 30 |
+
self.logger = logging.getLogger(self.__class__.__name__)
|
| 31 |
+
|
| 32 |
+
def call_agent(self, messages: List[AnyMessage]) -> BaseMessage:
|
| 33 |
+
response = self.llm.with_retry(stop_after_attempt=2).invoke(input=messages)
|
| 34 |
+
response.name = self.agent_name
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
async def acall_agent(self, messages: List[AnyMessage]) -> BaseMessage:
|
| 38 |
+
response = await self.llm.with_retry(stop_after_attempt=2).ainvoke(
|
| 39 |
+
input=messages
|
| 40 |
+
)
|
| 41 |
+
response.name = self.agent_name
|
| 42 |
+
return response
|
| 43 |
+
|
| 44 |
+
def call_agent_structured(
|
| 45 |
+
self, messages: List[AnyMessage], clazz: OutStruct
|
| 46 |
+
) -> OutStruct:
|
| 47 |
+
response = (
|
| 48 |
+
self.llm.with_structured_output(clazz)
|
| 49 |
+
.with_retry(stop_after_attempt=2)
|
| 50 |
+
.invoke(input=messages)
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return response
|
| 54 |
+
|
| 55 |
+
async def acall_agent_structured(
|
| 56 |
+
self, messages: List[AnyMessage], clazz: OutStruct
|
| 57 |
+
) -> OutStruct:
|
| 58 |
+
response = (
|
| 59 |
+
await self.llm.with_structured_output(clazz)
|
| 60 |
+
.with_retry(stop_after_attempt=2)
|
| 61 |
+
.ainvoke(input=messages)
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return response
|
pmcp/agents/trello_agent.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
|
| 3 |
+
from pmcp.agents.agent_base import AgentBlueprint
|
| 4 |
+
from langchain_core.tools import BaseTool
|
| 5 |
+
from langchain_core.messages import SystemMessage
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langgraph.graph import MessagesState
|
| 8 |
+
|
| 9 |
+
SYSTEM_PROMPT = """
|
| 10 |
+
You are an assistant that can manage Trello boards and projects.
|
| 11 |
+
You will be given a set of tools to work with. Each time you decide to use a tool that modifies in any way a Trello board, you MUST ask the user if wants to proceed.
|
| 12 |
+
If the user's answer is negative, then you have to abort everything and end the conversation.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TrelloAgent:
|
| 17 |
+
def __init__(self, tools: List[BaseTool], llm: ChatOpenAI):
|
| 18 |
+
self.__agent = AgentBlueprint(
|
| 19 |
+
agent_name="TRELLO_AGENT",
|
| 20 |
+
description="The agent that performs actions on Trello",
|
| 21 |
+
tools=tools,
|
| 22 |
+
system_prompt=SYSTEM_PROMPT.strip(),
|
| 23 |
+
llm=llm,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def call_trello_agent(self, state: MessagesState):
|
| 27 |
+
response = self.__agent.call_agent(
|
| 28 |
+
[SystemMessage(content=self.__agent.system_prompt)] + state.get("messages")
|
| 29 |
+
)
|
| 30 |
+
return {"messages": [response]}
|
| 31 |
+
|
| 32 |
+
async def acall_trello_agent(self, state: MessagesState):
|
| 33 |
+
response = await self.__agent.acall_agent(
|
| 34 |
+
[SystemMessage(content=self.__agent.system_prompt)] + state.get("messages")
|
| 35 |
+
)
|
| 36 |
+
return {"messages": [response]}
|
pyproject.toml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "pmcp"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"gradio[mcp]>=5.32.0",
|
| 9 |
+
"langchain-mcp-adapters>=0.1.1",
|
| 10 |
+
"langchain-openai>=0.3.18",
|
| 11 |
+
"langgraph>=0.4.7",
|
| 12 |
+
"langgraph-checkpoint-sqlite>=2.0.10",
|
| 13 |
+
"mcp>=1.9.0",
|
| 14 |
+
"smolagents[litellm,mcp,toolkit]>=1.17.0",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
[dependency-groups]
|
| 18 |
+
dev = [
|
| 19 |
+
"pre-commit>=4.2.0",
|
| 20 |
+
"ruff>=0.11.12",
|
| 21 |
+
]
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|