fix: address third round of review feedback
Add BasicAuthLifespanContext Protocol to make the contract between StdioContext and get_client() explicit and type-safe. Document why mcp.get_context() is required for non-template resources. Add News and Collectives to README Supported Apps table, fix transport default. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
1af85bc05e
commit
f340380898
@@ -1,6 +1,7 @@
|
||||
"""Helper functions for accessing context in MCP tools."""
|
||||
|
||||
import logging
|
||||
from typing import Protocol, runtime_checkable
|
||||
|
||||
from httpx import BasicAuth
|
||||
from mcp.server.fastmcp import Context
|
||||
@@ -14,6 +15,17 @@ from nextcloud_mcp_server.config import get_settings
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class BasicAuthLifespanContext(Protocol):
|
||||
"""Protocol for lifespan contexts that carry a shared NextcloudClient.
|
||||
|
||||
Implemented by :class:`~nextcloud_mcp_server.stdio.StdioContext` and
|
||||
the single-user lifespan context in ``app.py``.
|
||||
"""
|
||||
|
||||
client: NextcloudClient
|
||||
|
||||
|
||||
async def get_client(ctx: Context) -> NextcloudClient:
|
||||
"""
|
||||
Get the appropriate Nextcloud client based on authentication mode.
|
||||
@@ -59,7 +71,7 @@ async def get_client(ctx: Context) -> NextcloudClient:
|
||||
return await _get_client_from_login_flow(ctx, lifespan_ctx.nextcloud_host)
|
||||
|
||||
# BasicAuth mode - use shared client (no token exchange)
|
||||
if hasattr(lifespan_ctx, "client"):
|
||||
if isinstance(lifespan_ctx, BasicAuthLifespanContext):
|
||||
return lifespan_ctx.client
|
||||
|
||||
# OAuth multi-audience mode (has 'nextcloud_host' attribute)
|
||||
|
||||
@@ -17,6 +17,7 @@ from mcp.server.fastmcp import Context, FastMCP
|
||||
from nextcloud_mcp_server.client import NextcloudClient
|
||||
from nextcloud_mcp_server.config import get_settings
|
||||
from nextcloud_mcp_server.config_validators import AuthMode, validate_configuration
|
||||
from nextcloud_mcp_server.context import BasicAuthLifespanContext
|
||||
from nextcloud_mcp_server.context import get_client as get_nextcloud_client
|
||||
from nextcloud_mcp_server.server import AVAILABLE_APPS
|
||||
|
||||
@@ -24,12 +25,12 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class StdioContext:
|
||||
class StdioContext(BasicAuthLifespanContext):
|
||||
"""Minimal lifespan context for stdio transport.
|
||||
|
||||
Carries only the shared :class:`NextcloudClient`. The ``client``
|
||||
attribute satisfies the duck-type check in
|
||||
:func:`nextcloud_mcp_server.context.get_client`.
|
||||
Implements :class:`~nextcloud_mcp_server.context.BasicAuthLifespanContext`
|
||||
so that :func:`~nextcloud_mcp_server.context.get_client` recognises it
|
||||
as a single-user BasicAuth context.
|
||||
"""
|
||||
|
||||
client: NextcloudClient
|
||||
@@ -79,6 +80,9 @@ def get_stdio_mcp(enabled_apps: list[str] | None = None) -> FastMCP:
|
||||
mcp = FastMCP("Nextcloud MCP", lifespan=stdio_lifespan)
|
||||
|
||||
# --- capabilities resource (mirrors app.py) ---
|
||||
# NOTE: mcp.get_context() is required here because FastMCP's
|
||||
# FunctionResource (non-template resources) does not support
|
||||
# context parameter injection — only template resources do.
|
||||
@mcp.resource("nc://capabilities")
|
||||
async def nc_get_capabilities():
|
||||
"""Get the Nextcloud Host capabilities"""
|
||||
|
||||
Reference in New Issue
Block a user