A newer version of the Gradio SDK is available:
6.1.0
Sprint 3 (MVP 1): Implementing the Simplified Planner Agent
Sprint Overview
- Goal: Implement the
SimplePlannerAgentlogic that orchestrates theEmbeddingServiceandInMemoryKGto take a user query, process it, and return a list of suggestedMCPToolobjects. This sprint focuses on the agent's internal logic; UI integration comes in Sprint 4. - Duration: Estimated 2-4 hours (flexible within Hackathon Day 1 or early Day 2).
- Core Primitives Focused On: Tool (being suggested by the agent).
- Key Artifacts by End of Sprint:
agents/planner.py:SimplePlannerAgentclass fully implemented and tested.- Unit tests for
SimplePlannerAgent. - All code linted, formatted, type-checked, and passing CI.
Task List
Task 3.1: Implement SimplePlannerAgent Core Logic
- Status: Todo
- Parent MVP: MVP 1
- Parent Sprint (MVP 1): Sprint 3
- Description: In
agents/planner.py, fully implement theSimplePlannerAgentclass.- The
__init__method should accept instances ofInMemoryKGandEmbeddingService. - The
suggest_tools(self, user_query: str, top_k: int = 3) -> List[MCPTool]method should:- Handle empty or whitespace-only
user_query(return empty list). - Call
self.embedder.get_embedding(user_query)to get the query embedding. - If embedding fails (returns
None), log a warning and return an empty list. - Call
self.kg.find_similar_tools(query_embedding, top_k=top_k)to get relevanttool_ids. - For each
tool_idreturned, callself.kg.get_tool_by_id(tool_id)to retrieve theMCPToolobject. - Collect valid
MCPToolobjects and return them. - Add print statements or basic logging for observability during development.
- Handle empty or whitespace-only
- The
- Acceptance Criteria:
SimplePlannerAgentclass andsuggest_toolsmethod are fully implemented.- Method correctly utilizes injected
InMemoryKGandEmbeddingService. - Handles edge cases like empty queries or embedding failures gracefully.
- Unit tests (using mocks for dependencies) pass.
- TDD Approach: In
tests/agents/test_planner.py(create this file andtests/agents/__init__.py):test_planner_init: Test constructor.test_suggest_tools_empty_query: Assert returns[].test_suggest_tools_embedding_failure: MockEmbeddingService.get_embeddingto returnNone. Assert returns[]and logs a warning.test_suggest_tools_no_similar_tools_found: MockInMemoryKG.find_similar_toolsto return[]. Assertsuggest_toolsreturns[].test_suggest_tools_success:- Mock
EmbeddingService.get_embeddingto return a dummy query vector. - Mock
InMemoryKG.find_similar_toolsto return a list of dummy tool IDs (e.g.,["tool1", "tool2"]). - Mock
InMemoryKG.get_tool_by_idto return specificMCPToolinstances for "tool1" and "tool2", andNonefor any other ID. - Call
planner.suggest_tools("some query")and assert it returns the expected list ofMCPToolobjects.
- Mock
Task 3.2: Integration Point for Main Application (app.py)
- Status: Todo
- Parent MVP: MVP 1
- Parent Sprint (MVP 1): Sprint 3
- Description: In
app.py(where the Gradio app will be), add the initialization logic forInMemoryKG,EmbeddingService, andSimplePlannerAgent. This logic should be placed globally so it runs once when the Gradio app starts.- Load tools into the KG:
knowledge_graph.load_tools_from_json("data/initial_tools.json"). - Build the vector index:
knowledge_graph.build_vector_index(embedding_service). - Pass the initialized
knowledge_graphandembedding_serviceto theSimplePlannerAgentconstructor. - Handle potential errors during this initialization (e.g., API key not set, data file not found) by printing clear error messages.
- Load tools into the KG:
- Acceptance Criteria:
app.pycontains the global initialization block for KG, Embedder, and Planner.- Initialization sequence is correct (load data, then build index).
- Basic error handling for initialization is present.
- The app can be run (
python app.py) without crashing during this setup phase (Gradio UI itself is not built yet, but initialization should complete).
- TDD Approach: This is primarily setup code. Manual testing by running
python app.pyand checking console output is key. Unit tests for the individual components (KG, Embedder) should already cover their internal logic.
Task 3.3: Update Dependencies & Run All Checks
- Status: Todo
- Parent MVP: MVP 1
- Parent Sprint (MVP 1): Sprint 3
- Description:
- Review
requirements.txtandrequirements-dev.txtfor any new additions (e.g.,python-dotenvif used for local API key loading). - Regenerate
requirements.lock:uv pip compile requirements.txt requirements-dev.txt --all-extras -o requirements.lock. - Run
just install(oruv pip sync requirements.lock). - Run
just lint,just format,just type-check,just test. - Commit all changes.
- Push to GitHub and verify CI pipeline passes.
- Review
- Acceptance Criteria:
requirements.lockis updated.- All
justchecks pass locally. - Code committed and pushed.
- GitHub Actions CI pipeline passes for the sprint's commits.
Implementation Guidance
Task 3.1 Implementation Details
# In agents/planner.py
from typing import List, Optional
from kg_services.ontology import MCPTool
from kg_services.knowledge_graph import InMemoryKG
from kg_services.embedder import EmbeddingService
class SimplePlannerAgent:
"""
A simplified planner agent that suggests tools based on user queries
using semantic similarity search.
"""
def __init__(self, kg: InMemoryKG, embedder: EmbeddingService):
"""Initialize the planner with knowledge graph and embedding service."""
self.kg = kg
self.embedder = embedder
def suggest_tools(self, user_query: str, top_k: int = 3) -> List[MCPTool]:
"""
Suggest relevant tools based on user query using semantic similarity.
Args:
user_query: Natural language query from user
top_k: Maximum number of tools to suggest
Returns:
List of relevant MCPTool objects, ordered by relevance
"""
# Handle empty or whitespace-only queries
if not user_query or not user_query.strip():
print("Warning: Empty or whitespace-only query provided")
return []
# Get embedding for the user query
query_embedding = self.embedder.get_embedding(user_query)
if query_embedding is None:
print(f"Warning: Could not generate embedding for query: {user_query}")
return []
# Find similar tools using the knowledge graph
similar_tool_ids = self.kg.find_similar_tools(query_embedding, top_k=top_k)
# Retrieve actual MCPTool objects
suggested_tools: List[MCPTool] = []
for tool_id in similar_tool_ids:
tool = self.kg.get_tool_by_id(tool_id)
if tool is not None:
suggested_tools.append(tool)
print(f"Planner suggested tools: {[t.name for t in suggested_tools]} for query: '{user_query}'")
return suggested_tools
Task 3.2 Implementation Details
# In app.py - Global Service Initialization Section
print("Attempting to initialize KGraph-MCP services...")
embedding_service_instance = None
knowledge_graph_instance = None
planner_agent_instance = None
try:
# For local dev, ensure .env is loaded if API keys are there
# from dotenv import load_dotenv
# load_dotenv() # Add python-dotenv to requirements-dev.txt if needed
from kg_services.embedder import EmbeddingService
from kg_services.knowledge_graph import InMemoryKG
from agents.planner import SimplePlannerAgent
embedding_service_instance = EmbeddingService()
knowledge_graph_instance = InMemoryKG()
# Load initial tools data
print("Loading tools from data/initial_tools.json...")
knowledge_graph_instance.load_tools_from_json("data/initial_tools.json")
# Build vector index (this will make actual API calls)
print("Building vector index (may take a moment for first run)...")
knowledge_graph_instance.build_vector_index(embedding_service_instance)
print("Vector index built successfully.")
# Initialize the planner agent
planner_agent_instance = SimplePlannerAgent(knowledge_graph_instance, embedding_service_instance)
print("KGraph-MCP services initialized successfully.")
except FileNotFoundError as e:
print(f"FATAL: Data file not found: {e}")
print("Please ensure data/initial_tools.json exists.")
except Exception as e:
print(f"FATAL: Error during service initialization: {e}")
print("The application may not function correctly.")
# In a real app, you might want to prevent Gradio from launching or show an error state.
Task 3.3 Implementation Details
This task is primarily operational:
Dependency Review:
# Check if python-dotenv is needed grep -r "dotenv" . --include="*.py" # If found, add to requirements-dev.txtQuality Checks:
# Update dependencies uv pip compile requirements.txt requirements-dev.txt --all-extras -o requirements.lock uv pip sync requirements.lock # Run all quality checks just lint just format just type-check just testCommit and Push:
# Stage all changes git add -A # Use conventional commit python scripts/smart_commit.py "implement SimplePlannerAgent and app integration" # Push to trigger CI git push origin main
End of Sprint 3 Review
What's Done:
- The
SimplePlannerAgentis implemented, capable of taking a user query and using theEmbeddingServiceandInMemoryKGto produce a list of relevantMCPToolobjects. - The main application (
app.py) now initializes all backend services on startup. - Unit tests cover the planner agent's logic using mocks.
- The backend logic for the "KG-Powered Tool Suggester" is now complete.
What's Next (Sprint 4):
- Implement the Gradio UI in
app.pyto take user input and display the suggestions from theSimplePlannerAgent. - Create interactive demo interface for the hackathon.
- Add visualization components for the knowledge graph.
Key Benefits Achieved:
- Separation of Concerns: Agent logic is decoupled from UI
- Testability: Comprehensive unit tests with mocked dependencies
- Robustness: Proper error handling for edge cases
- Integration Ready: Clean interfaces for UI integration in Sprint 4
Potential Blockers/Issues:
- API key configuration for embedding service
- Performance of vector index building with real API calls
- Memory usage with larger tool datasets
Implementation Priority: This sprint establishes the core intelligence of MVP1. The next sprint will expose this functionality through an interactive user interface, completing the hackathon demo.