Commit Graph

177 Commits

Author SHA1 Message Date
Henry Mao
9d65e5ac03 Remove optional check 2025-03-07 22:02:16 +08:00
Henry Mao
fd826cc7a6 Fix Websocket Client and Add Test 2025-03-07 22:00:56 +08:00
Henry Mao
fc021eea76 Apply suggestions from code review
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2025-03-06 20:12:51 +08:00
Henry Mao
f67c3ee6c0 Merge branch 'modelcontextprotocol:main' into patch-1 2025-03-06 13:24:01 +08:00
David Soria Parra
b1942b31c4 Fix #177: Returning multiple tool results (#222)
* feat: allow lowlevel servers to return a list of resources

The resource/read message in MCP allows of multiple resources
to be returned. However, in the SDK we do not allow this. This
change is such that we allow returning multiple resource in
the lowlevel API if needed. However in FastMCP we stick to
one, since a FastMCP resource defines the mime_type in the decorator
and hence a resource cannot dynamically return different mime_typed resources.
It also is just the better default to only return one resource.
However in the lowlevel API we will allow this.

Strictly speaking this is not a BC break since the new return value
is additive, but if people subclassed server, it will break them.

* feat: lower the type requriements for call_tool to Iterable
2025-02-20 21:31:26 +00:00
Jerome
2628e01f4b Merge pull request #217 from modelcontextprotocol/jerome/fix/request-context-typing
Updated typing on request context for the server to use server session
2025-02-20 11:07:54 +00:00
David Soria Parra
a50cf92d3a fix: ruff format 2025-02-20 11:04:46 +00:00
Jerome
ff22f48365 Add client handling for sampling, list roots, ping (#218)
Adds sampling and list roots callbacks to the ClientSession, allowing the client to handle requests from the server.

Co-authored-by: TerminalMan <84923604+SecretiveShell@users.noreply.github.com>
Co-authored-by: David Soria Parra <davidsp@anthropic.com>
2025-02-20 10:49:43 +00:00
Randall Nortman
106619967b Force stdin/stdout encoding to UTF-8
The character encoding of the stdin/stdout streams in Python is platform-
dependent. On Windows it will be something weird, like CP437 or CP1252,
depending on the locale. This change ensures that no matter the platform,
UTF-8 is used.
2025-02-18 11:23:24 +00:00
Henry Wildermuth
b53e090299 Update URL validation to allow file and other nonstandard schemas 2025-02-13 14:58:58 -08:00
David Soria Parra
fbf4acc679 fix: method ordering 2025-02-13 14:54:53 +00:00
Jerome
da53a97ed9 Made message handling concurrent 2025-02-13 14:53:08 +00:00
David Soria Parra
4d3e05f6f6 refactor: improve lifespan context typing and documentation
- Add proper generic parameter for lifespan context type
- Update README with TypedDict example for strong typing
- Fix context variable initialization in server
- Improve property return type safety
- Remove redundant documentation
- Ensure compatibility with existing tests
2025-02-13 10:12:59 +00:00
David Soria Parra
fddba00723 refactor: improve server context management with AsyncExitStack
Replace nested context managers with AsyncExitStack to ensure proper cleanup
order during server shutdown and make the code more maintainable.
2025-02-11 12:26:32 +00:00
David Soria Parra
d3ea9009b0 feat: add lifespan support to FastMCP server
Adds support for the lifespan API to FastMCP server, enabling:
- Simple setup with FastMCP constructor
- Type-safe context passing to tools and handlers
- Configuration via Settings class
2025-02-11 12:16:44 +00:00
David Soria Parra
2c7bd8343e feat: add lifespan support to low-level MCP server
Adds a context manager based lifespan API in mcp.server.lowlevel.server to manage server lifecycles in a
type-safe way. This enables servers to:
- Initialize resources on startup and clean them up on shutdown
- Pass context data from startup to request handlers
- Support async startup/shutdown operations
2025-02-11 12:16:40 +00:00
David Soria Parra
c58adfe3f5 Merge pull request #167 from modelcontextprotocol/davidsp/88-v2
feat: add request cancellation and cleanup
2025-02-04 20:23:05 +00:00
David Soria Parra
733db0c9cf fix: enforce context manager usage for RequestResponder 2025-02-04 20:13:26 +00:00
David Soria Parra
08cfbe522a fix: improve error handling and request cancellation for issue #88 2025-02-04 18:20:17 +00:00
zzstoatzz
ca060014bb centralize type 2025-02-04 11:11:38 -06:00
zzstoatzz
00a44692e7 init 2025-02-04 11:11:38 -06:00
David Soria Parra
827e494df4 feat: add request cancellation and in-flight request tracking
This commit adds support for request cancellation and tracking of
in-flight requests in the MCP protocol implementation. The key
architectural changes are:

1. Request Lifecycle Management:
   - Added _in_flight dictionary to BaseSession to track active requests
   - Requests are tracked from receipt until completion/cancellation
   - Added proper cleanup via on_complete callback

2. Cancellation Support:
   - Added CancelledNotification handling in _receive_loop
   - Implemented cancel() method in RequestResponder
   - Uses anyio.CancelScope for robust cancellation
   - Sends error response on cancellation

3. Request Context:
   - Added request_ctx ContextVar for request context
   - Ensures proper cleanup after request handling
   - Maintains request state throughout lifecycle

4. Error Handling:
   - Improved error propagation for cancelled requests
   - Added proper cleanup of cancelled requests
   - Maintains consistency of in-flight tracking

This change enables clients to cancel long-running requests and
servers to properly clean up resources when requests are cancelled.

Github-Issue:#88
2025-02-03 20:50:05 +00:00
David Soria Parra
0d3e02f6a2 fix: standardize resource response format
Github-Issue:#141
2025-02-03 11:33:32 +00:00
David Soria Parra
5e19c7cf73 Merge pull request #176 from sheffler/fixFirstProgressReport
progress_token is 0 on first tool-call and the return is taken mistakenly
2025-01-30 16:00:01 +00:00
Henry Mao
1395607583 Create Client websocket.py 2025-01-29 21:27:09 +08:00
David Soria Parra
53bfac0cc2 fix: make progress_token check E711 compat 2025-01-29 10:01:56 +00:00
Thomas Sheffler
76325b7846 progress_token is 0 on first tool-call and the return is taken mistakenly 2025-01-27 19:04:20 -08:00
David Soria Parra
070e8412c0 refactor: standardize resource response format
Introduce ReadResourceContents type to properly handle MIME types in resource responses. Breaking change in FastMCP read_resource() return type.

Github-Issue:#152
2025-01-27 20:36:10 +00:00
David Soria Parra
8ff4b5e9d3 fix: respect resource mime type in responses
The server was ignoring mime types set on resources, defaulting to text/plain
for strings and application/octet-stream for bytes. Now properly preserves
the specified mime type in both FastMCP and low-level server implementations.

Note that this is breaks backwards compatibility as it changes the return
values of read_resource() on FastMCP. It is BC compatible on lowlevel since
it only extends the callback.

Github-Issue: #152
Reported-by: eiseleMichael
2025-01-27 15:51:44 +00:00
Thomas Sheffler
7a64d8f9af async and await for logging in fastmcp 2025-01-25 07:03:20 -08:00
David Soria Parra
0a93799068 refactor: extract request and notification handling into separate methods 2025-01-24 09:52:20 +00:00
David Soria Parra
bd742272ab fix: update types to reflext 2024-11-05 schema 2025-01-24 09:52:16 +00:00
David Soria Parra
0cb3b26514 Merge pull request #157 from micpst/async-resources
feat: add async resources for FastMCP
2025-01-22 13:37:41 +00:00
Michał Pstrąg
c184b7a8db fix fastmcp logger debug 2025-01-18 19:59:51 +01:00
Michał Pstrąg
f5f19b2361 add support for async resources 2025-01-18 19:37:39 +01:00
Salman Mohammed
bc6746e89b Add instructions field to ServerSession and FastMCP 2025-01-13 13:00:11 -05:00
Salman Mohammed
135191403a Add instructions field to InitializeResult 2025-01-13 11:58:43 -05:00
David Soria Parra
4770bcd0b1 fix: #129 resource template handling in FastMCP server 2025-01-06 20:02:03 +00:00
David Soria Parra
a77aaa79a4 refactor: use built-in typing.Annotated instead of typing_extensions 2025-01-06 10:47:44 +00:00
David Soria Parra
e8dd39f839 fix: #128 fix package in the generated mcp run config
During `mcp install` we are generating a config
that relies on `mcp run`. However `mcp` is only available
as a CLI command if we use the `cli` feature.

We need to specify that we want to install mcp
with the cli feature.

Fixes #128
2025-01-06 10:37:01 +00:00
David Soria Parra
58bc32e559 merge: Resolve conflicts from v1.1.x merge 2025-01-03 16:06:51 +00:00
David Soria Parra
e21e8c157d style: Fix import sorting and update ruff version 2025-01-03 15:57:14 +00:00
David Soria Parra
d93b99e163 Merge pull request #121 from allenporter/mcp-sse-type-error
Fix SSE server uncaught TypeError due to incorrect routing
2025-01-03 14:38:58 +00:00
David Soria Parra
ae73191b3e fix: Change --env-var short flag from -e to -v to avoid conflict with --with-editable 2025-01-02 16:57:53 +00:00
David Soria Parra
bd5170e19e Merge pull request #120 from SecretiveShell/fix-encoding-errors
add text encoding params to STDIO client
2025-01-02 14:59:36 +00:00
Allen Porter
b89cabc7df fix: Add constructor for McpError to allow setting field
Backport of #116 by @allenporter to v1.1.x branch.

Fixes an issue where exception handling code fails with AttributeError
when accessing the error field of McpError.
2025-01-02 09:32:43 +00:00
restlessronin
3de4dc1f13 feat: add version string parameter to Server constructor 2025-01-02 09:11:07 +00:00
restlessronin
34a257147b chore: revert doc changes 2025-01-02 09:11:07 +00:00
restlessronin
937c640f04 feat: add version string parameter to 'create_initialization_options' 2025-01-02 09:11:07 +00:00
Allen Porter
247aec9332 Update paths in examples 2024-12-30 10:09:42 -08:00