-
Notifications
You must be signed in to change notification settings - Fork 2.6k
(experiment) sub-recipes prototype #2862
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| build | ||
| subrecipes_mcp.egg-info/ |
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 @@ | ||
| an MCP server that runs goose recipes, to be used in a prototype for subrecipe implementaiton. | ||
|
|
||
| install using: | ||
|
|
||
| uv tool install -e . |
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,83 @@ | ||
| import argparse | ||
| import asyncio | ||
| import json | ||
| from typing import Awaitable, Callable | ||
| from pathlib import Path | ||
| from fastmcp import FastMCP, Context | ||
| from mcp import types | ||
|
|
||
| server = FastMCP( | ||
| "Subrecipes 🚀", | ||
| instructions="This extension allows you to run subrecipes.", | ||
| ) | ||
|
|
||
| del server._mcp_server.request_handlers[ | ||
| types.ListResourcesRequest | ||
| ] # Otherwise the Goose system prompt says this supports resources | ||
|
|
||
|
|
||
| def runner(subrecipe: dict) -> tuple[str, Callable[[Context], Awaitable[str]]]: | ||
| path = Path(subrecipe["path"]) | ||
| name = ( | ||
| "_".join(path.parts[-2:]) | ||
| .removesuffix(".yaml") | ||
| .removesuffix(".yml") | ||
| .removesuffix(".json") | ||
| )[:64] # Claude rejected a name for being longer than 64 characters | ||
| name = name.replace(".", "_").replace(" ", "_") | ||
|
|
||
| async def tool_func(ctx: Context) -> str: | ||
| await ctx.info(f"Running subrecipe {name}") | ||
|
|
||
| # shell out to the subrecipe and stream stdout/stderr | ||
| process = await asyncio.create_subprocess_exec( | ||
| "goose", | ||
| "run", | ||
| "--recipe", | ||
| path, | ||
| stdout=asyncio.subprocess.PIPE, | ||
| stderr=asyncio.subprocess.PIPE, | ||
| ) | ||
|
|
||
| output = [] | ||
|
|
||
| async def read_stream(stream, name): | ||
| while True: | ||
| line = await stream.readline() | ||
| if not line: | ||
| break | ||
| if line := line.decode("utf-8").rstrip(): | ||
| await ctx.log(f"{name}: {line}") | ||
| output.append(line) | ||
|
|
||
| await asyncio.gather( | ||
| read_stream(process.stdout, "stdout"), | ||
| read_stream(process.stderr, "stderr"), | ||
| ) | ||
|
|
||
| return_code = await process.wait() | ||
| return f"Subrecipe {name} finished with return code {return_code}" | ||
|
|
||
| return name, tool_func | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--subrecipes-json", type=str, required=True) | ||
| args = parser.parse_args() | ||
|
|
||
| subrecipes_json = json.loads(args.subrecipes_json) | ||
|
|
||
| for subrecipe in subrecipes_json: | ||
| name, tool_func = runner(subrecipe) | ||
| server.tool( | ||
| tool_func, | ||
| name=name, | ||
| description=f"Run {name}", | ||
| ) | ||
|
|
||
| server.run() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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,11 @@ | ||
| [project] | ||
| name = "subrecipes-mcp" | ||
| version = "0.1.0" | ||
| description = "Add your description here" | ||
| requires-python = ">=3.13" | ||
| dependencies = [ | ||
| "fastmcp>=2.3.4", | ||
| ] | ||
|
|
||
| [project.scripts] | ||
| subrecipes-mcp = "main:main" |
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,25 @@ | ||
| version: 1.0.0 | ||
| title: "Goose Recipe Handler" | ||
| description: "A simple request router using sub-recipes" | ||
|
|
||
| prompt: | | ||
| You are tasked with answering incoming requests and taking action. | ||
| Read the oldest file in 'inbox' and see if you can help. | ||
|
|
||
| Use the tools available to you to process the request. | ||
|
|
||
| Write replies to the 'oubox' folder, with the same name as the file you read from 'inbox'. | ||
|
|
||
| If there is no appropriate action to take, just reply stating that you cannot help. | ||
|
|
||
| If you can help, respond with the result of your action. | ||
|
|
||
| Then "touch" the file in 'inbox' to mark it as processed. | ||
|
|
||
| extensions: | ||
| - type: builtin # just to read the file | ||
| name: developer | ||
|
|
||
| subrecipes: | ||
| - path: ../goose-recipes/joke-of-the-day/recipe.yaml | ||
| - path: ../goose-recipes/tutorial-trip-planner/recipe.yaml |
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.
@lifeizhou-ap my thinking is that we turn these into tools added and handled by Goose itself, without the MCP layer at all. Then we can start with an implementation where Goose still shells out to itself. And that leaves the door open to Goose running the subrecipe in its own separate agent in-process