-
Notifications
You must be signed in to change notification settings - Fork 345
[Feature] New API to discover, fetch, and call tools from server extensions #1521
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
Draft
Abigayle-Mercer
wants to merge
8
commits into
jupyter-server:main
Choose a base branch
from
Abigayle-Mercer:feature/tool-discovery-api
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2fbfbf6
added tools property to ExtensionPoint, get_tools to ExtensionManager…
abmercer d44f68c
added an API for listing tools in serveres/contents/handlers.py
abmercer f6a5d42
added some comments about uncertainties in the logic / layout
abmercer 56b1bfd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 715e76e
moved ListToolInforHandler to separate service, fixed doc strings, cl…
abmercer f4fe448
moved ListToolInforHandler to separate service, fixed doc strings, cl…
abmercer 24116cd
the tools property now allows extensions to show up with their own va…
abmercer 9b23590
added some preliminary unit testing for ListToolHandler, tool propert…
abmercer 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from tornado import web | ||
from jupyter_server.base.handlers import APIHandler | ||
|
||
class ListToolInfoHandler(APIHandler): | ||
@web.authenticated | ||
async def get(self): | ||
tools = self.serverapp.extension_manager.discover_tools() | ||
self.finish({"discovered_tools": tools}) | ||
|
||
|
||
|
||
default_handlers = [ | ||
(r"/api/tools", ListToolInfoHandler), | ||
] |
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,22 @@ | ||
"""A mock extension exposing a structured tool.""" | ||
|
||
def jupyter_server_extension_tools(): | ||
return { | ||
"mock_tool": { | ||
"metadata": { | ||
"name": "mock_tool", | ||
"description": "A mock tool for testing.", | ||
"inputSchema": { | ||
"type": "object", | ||
"properties": { | ||
"input": {"type": "string"} | ||
}, | ||
"required": ["input"] | ||
} | ||
}, | ||
"callable": lambda input: f"Echo: {input}" | ||
} | ||
} | ||
|
||
def _load_jupyter_server_extension(serverapp): | ||
serverapp.log.info("Loaded mock tool extension.") |
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,21 @@ | ||
"""A mock extension that defines a duplicate tool name to test conflict handling.""" | ||
|
||
def jupyter_server_extension_tools(): | ||
return { | ||
"mock_tool": { # <-- duplicate on purpose | ||
"metadata": { | ||
"name": "mock_tool", | ||
"description": "Conflicting tool name.", | ||
"inputSchema": { | ||
"type": "object", | ||
"properties": { | ||
"input": {"type": "string"} | ||
} | ||
} | ||
}, | ||
"callable": lambda input: f"Echo again: {input}" | ||
} | ||
} | ||
|
||
def _load_jupyter_server_extension(serverapp): | ||
serverapp.log.info("Loaded dupe tool extension.") |
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,39 @@ | ||
"""A mock extension that provides a custom validation schema.""" | ||
|
||
OPENAI_TOOL_SCHEMA = { | ||
"type": "object", | ||
"properties": { | ||
"name": {"type": "string"}, | ||
"description": {"type": "string"}, | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"input": {"type": "string"} | ||
}, | ||
"required": ["input"] | ||
} | ||
}, | ||
"required": ["name", "parameters"] | ||
} | ||
|
||
def jupyter_server_extension_tools(): | ||
tools = { | ||
"openai_style_tool": { | ||
"metadata": { | ||
"name": "openai_style_tool", | ||
"description": "Tool using OpenAI-style parameters", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"input": {"type": "string"} | ||
}, | ||
"required": ["input"] | ||
} | ||
}, | ||
"callable": lambda input: f"Got {input}" | ||
} | ||
} | ||
return (tools, OPENAI_TOOL_SCHEMA) | ||
|
||
def _load_jupyter_server_extension(serverapp): | ||
serverapp.log.info("Loaded mock custom-schema extension.") |
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,29 @@ | ||
import json | ||
import pytest | ||
|
||
@pytest.fixture | ||
def jp_server_config(): | ||
return { | ||
"ServerApp": { | ||
"jpserver_extensions": { | ||
"tests.extension.mockextensions.mockext_tool": True, | ||
"tests.extension.mockextensions.mockext_customschema": True, | ||
} | ||
} | ||
} | ||
|
||
@pytest.mark.asyncio | ||
async def test_multiple_tools_present(jp_fetch): | ||
response = await jp_fetch("api", "tools", method="GET") | ||
assert response.code == 200 | ||
|
||
body = json.loads(response.body.decode()) | ||
tools = body["discovered_tools"] | ||
|
||
# Check default schema tool | ||
assert "mock_tool" in tools | ||
assert "inputSchema" in tools["mock_tool"] | ||
|
||
# Check custom schema tool | ||
assert "openai_style_tool" in tools | ||
assert "parameters" in tools["openai_style_tool"] |
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.
Instead of using
print
, we should make this aself.log.error
.Before we do that, though, we'll need to make
ExtensionPoint
aLoggingConfigurable
and set theparent
trait toExtensionPackage
. I can work on this in a separate PR and we can rebase here once that's merged.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.
See #1522. If/when that gets merged, we can rebase your PR to use the logger created there.