-
Notifications
You must be signed in to change notification settings - Fork 4.1k
DRAFT docs, ch3 - elicitation #335
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
softchris
wants to merge
6
commits into
main
Choose a base branch
from
ch3.10-elicitation
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a60ba2
first commit
softchris 811e9f4
Update 03-GettingStarted/10-elicitation/code/python/server.py
leestott 99b63e7
adding typescript pivot + asset
softchris c7494d0
Merge remote-tracking branch 'refs/remotes/origin/ch3.10-elicitation'…
softchris 7709868
completed typescript vertical in code directory
softchris 602781b
adding complete code pivots, assignment is just scaffolded thogh
softchris 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Elicitation |
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,56 @@ | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from mcp.server.fastmcp import Context, FastMCP | ||
| from mcp.server.session import ServerSession | ||
|
|
||
| from starlette.applications import Starlette | ||
| from starlette.routing import Mount, Host | ||
| from mcp.server.fastmcp import FastMCP | ||
|
|
||
| mcp = FastMCP(name="Elicitation Example") | ||
|
|
||
| # todo: elicitation example, turn it into sse | ||
|
|
||
| class BookingPreferences(BaseModel): | ||
| """Schema for collecting user preferences.""" | ||
|
|
||
| checkAlternative: bool = Field(description="Would you like to check another date?") | ||
| alternativeDate: str = Field( | ||
| default="2024-12-26", | ||
| description="Alternative date (YYYY-MM-DD)", | ||
| ) | ||
|
|
||
| def not_available_date(date: str) -> bool: | ||
| # Simulate date availability check | ||
| return date != "2024-12-25" | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| async def book_trip(date: str, ctx: Context[ServerSession, None]) -> str: | ||
| """Book a trip with date availability check.""" | ||
| # Check if date is available | ||
| if not_available_date(date): | ||
| # Date unavailable - ask user for alternative | ||
| result = await ctx.elicit( | ||
| message=(f"No trips available on {date}. Would you like to try another date?"), | ||
| schema=BookingPreferences, | ||
| ) | ||
|
|
||
| if result.action == "accept" and result.data: | ||
| if result.data.checkAlternative: | ||
| return f"[SUCCESS] Booked for {result.data.alternativeDate}" | ||
| return "[CANCELLED] No booking made" | ||
| return "[CANCELLED] Booking cancelled" | ||
|
|
||
| # Date available | ||
| return f"[SUCCESS] Booked for {date}" | ||
|
|
||
| app = Starlette( | ||
| routes=[ | ||
| Mount('/', app=mcp.sse_app()), | ||
| ] | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| print("Starting Elicitation Example MCP Server...") | ||
| mcp.run() | ||
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 @@ | ||
| // server for elicitation | ||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.