generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 79
feat: add MCP tools for SOP discovery and retrieval #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
konippi
wants to merge
4
commits into
strands-agents:main
Choose a base branch
from
konippi:add-mcp-tools-for-sop-discovery-and-retrieval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9fb664d
feat: add MCP tools for SOP discovery and retrieval
konippi fad2646
chore: fix conflict with main
konippi c134079
Merge branch 'main' into add-mcp-tools-for-sop-discovery-and-retrieval
jlhood 32b6934
refactor(mcp): consolidate MCP server and simplify SOP tools
konippi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """MCP server implementation for Agent SOPs.""" | ||
|
|
||
| from .server import AgentSOPMCPServer | ||
|
|
||
| __all__ = ["AgentSOPMCPServer"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """MCP server orchestrator for Agent SOPs.""" | ||
|
|
||
| import logging | ||
| from collections.abc import Callable | ||
|
|
||
| from mcp.server.fastmcp import FastMCP | ||
|
|
||
| from ..utils import load_sops | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AgentSOPMCPServer: | ||
| """MCP server for serving Agent SOPs as prompts and tools.""" | ||
|
|
||
| def __init__(self, sop_paths: str | None = None): | ||
| """Initialize the MCP server. | ||
|
|
||
| Args: | ||
| sop_paths: Optional colon-separated string of external SOP directory paths | ||
| """ | ||
| self.sop_paths = sop_paths | ||
| self.mcp = FastMCP("agent-sop-prompt-server") | ||
| self.sops = {sop["name"]: sop for sop in load_sops(self.sop_paths)} | ||
| self._register_sop_prompts(self.sops) | ||
| self._register_sop_tools() | ||
|
|
||
| def _register_sop_tools(self) -> None: | ||
| """Register SOP management tools with MCP server.""" | ||
|
|
||
| @self.mcp.tool() | ||
| def list_agent_sops() -> list[dict]: | ||
| """List all available agent SOPs with name and description. | ||
|
|
||
| Returns: | ||
| List of SOP dictionaries containing name and description | ||
| """ | ||
| result = [] | ||
| for sop in self.sops.values(): | ||
| result.append( | ||
| { | ||
| "name": sop["name"], | ||
| "description": sop["description"], | ||
| } | ||
| ) | ||
| return result | ||
|
|
||
| @self.mcp.tool() | ||
| def get_agent_sop(name: str) -> dict: | ||
| """Get the full content of a specific SOP. | ||
|
|
||
| Args: | ||
| name: Name of the SOP to retrieve | ||
|
|
||
| Returns: | ||
| SOP dictionary with name, description, and full content | ||
|
|
||
| Raises: | ||
| ValueError: If SOP name is not found | ||
| """ | ||
| if name in self.sops: | ||
| sop = self.sops[name] | ||
| return { | ||
| "name": sop["name"], | ||
| "description": sop["description"], | ||
| "content": sop["content"], | ||
| } | ||
|
|
||
| raise ValueError( | ||
| f"SOP '{name}' not found. Available SOPs: {list(self.sops.keys())}" | ||
| ) | ||
|
|
||
| def run(self) -> None: | ||
| """Start the MCP server.""" | ||
| self.mcp.run() | ||
|
|
||
| def _register_sop_prompts(self, sops: dict[str, dict]) -> None: | ||
| """Register SOP prompts with MCP server. | ||
|
|
||
| Args: | ||
| sops: Dictionary of SOP name to SOP dict with name, description, and content | ||
| """ | ||
| for sop in sops.values(): | ||
| try: | ||
| handler = self._create_prompt_handler(sop["name"], sop["content"]) | ||
| self.mcp.prompt(name=sop["name"], description=sop["description"])(handler) | ||
| except Exception as e: | ||
| raise RuntimeError( | ||
| f"Error registering prompt for SOP '{sop['name']}': {e}" | ||
| ) from e | ||
|
|
||
| def _create_prompt_handler(self, sop_name: str, sop_content: str) -> Callable[[str], str]: | ||
| """Create a prompt handler for a specific SOP. | ||
|
|
||
| Args: | ||
| sop_name: Name of the SOP | ||
| sop_content: Content of the SOP | ||
|
|
||
| Returns: | ||
| Prompt handler function | ||
| """ | ||
|
|
||
| def get_prompt(user_input: str = "") -> str: | ||
| return f"""Run this SOP: | ||
| <agent-sop name="{sop_name}"> | ||
| <content> | ||
| {sop_content} | ||
| </content> | ||
| <user-input> | ||
| {user_input} | ||
| </user-input> | ||
| </agent-sop>""" | ||
|
|
||
| return get_prompt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there is a lot of duplicated logic in this functions and the
load_external_sops, and the https://github.com/strands-agents/agent-sop/blob/main/python/strands_agents_sops/__init__.py file. Can we better consolidate this logic?