codeocean-mcp-server 0.12.2__tar.gz → 0.13.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/CHANGELOG.md +11 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/PKG-INFO +6 -4
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/README.md +3 -1
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/pyproject.toml +5 -4
- codeocean_mcp_server-0.13.1/src/codeocean_mcp_server/logging_config.py +70 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/server.py +2 -1
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/tools/capsules.py +47 -1
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/tools/data_assets.py +6 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/test_http_transport.py +49 -21
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/test_logging_config.py +36 -5
- codeocean_mcp_server-0.13.1/uv.lock +3842 -0
- codeocean_mcp_server-0.12.2/src/codeocean_mcp_server/logging_config.py +0 -67
- codeocean_mcp_server-0.12.2/uv.lock +0 -3274
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/.gitignore +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/.python-version +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/LICENSE +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/RELEASE.md +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/__init__.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/client.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/file_utils.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/models.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/search.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/tools/__init__.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/tools/computations.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/tools/custom_metadata.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/bedrock_call.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/bedrock_tools_converter.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/conftest.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/mcp_client.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/test_multi_tool_calling.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/test_search.py +0 -0
- {codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/tests/test_tool_calling.py +0 -0
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
CHANGELOG
|
|
2
2
|
=========
|
|
3
3
|
|
|
4
|
+
## 0.13.1 (2026-09-16)
|
|
5
|
+
|
|
6
|
+
- [#49](https://github.com/codeocean/codeocean-mcp-server/pull/49) feat: take the log level from `LOG_LEVEL`
|
|
7
|
+
|
|
8
|
+
## 0.13.0 (2026-09-16)
|
|
9
|
+
|
|
10
|
+
- [#46](https://github.com/codeocean/codeocean-mcp-server/pull/46) fix: bump mcp to 1.29.x and refresh locked dependencies
|
|
11
|
+
- [#37](https://github.com/codeocean/codeocean-mcp-server/pull/37) feat: add capsule and pipeline release tools
|
|
12
|
+
- [#47](https://github.com/codeocean/codeocean-mcp-server/pull/47) feat: add permissions and Git sync tools
|
|
13
|
+
- **Minimum Code Ocean platform version updated to `4.8.0`.**
|
|
14
|
+
|
|
4
15
|
## 0.12.2 (2026-09-03)
|
|
5
16
|
|
|
6
17
|
- [#42](https://github.com/codeocean/codeocean-mcp-server/pull/42) fix: run synchronous tool bodies in a worker thread
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: codeocean-mcp-server
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.13.1
|
|
4
4
|
Summary: Code Ocean MCP Server
|
|
5
5
|
Project-URL: Homepage, https://github.com/codeocean/codeocean-mcp-server
|
|
6
6
|
Project-URL: Issues, https://github.com/codeocean/codeocean-mcp-server/issues
|
|
@@ -13,15 +13,15 @@ Classifier: Operating System :: OS Independent
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3
|
|
14
14
|
Requires-Python: >=3.10
|
|
15
15
|
Requires-Dist: anyio>=4.5
|
|
16
|
-
Requires-Dist: codeocean<0.
|
|
17
|
-
Requires-Dist: mcp<1.
|
|
16
|
+
Requires-Dist: codeocean<0.18.0,>=0.17.0
|
|
17
|
+
Requires-Dist: mcp<1.30.0,>=1.29.1
|
|
18
18
|
Description-Content-Type: text/markdown
|
|
19
19
|
|
|
20
20
|
# Code Ocean MCP Server
|
|
21
21
|
|
|
22
22
|
Model Context Protocol (MCP) server for Code Ocean.
|
|
23
23
|
|
|
24
|
-
This MCP server provides tools to search and
|
|
24
|
+
This MCP server provides tools to search, run, and release capsules and pipelines, and manage data assets.
|
|
25
25
|
|
|
26
26
|
## Table of Contents
|
|
27
27
|
|
|
@@ -249,3 +249,5 @@ This will start a web server where you can:
|
|
|
249
249
|
The MCP server supports custom log formatting through the `LOG_FORMAT` environment variable. This allows you to control the format of log messages output by the server.
|
|
250
250
|
**Example Format Strings:** `"%(asctime)s %(levelname)s [%(name)s] %(message)s"`.
|
|
251
251
|
If `LOG_FORMAT` is not set, the server uses FastMCP's default logging configuration.
|
|
252
|
+
|
|
253
|
+
The `LOG_LEVEL` environment variable sets the level the server logs at: `DEBUG`, `INFO` (the default), `WARNING`, `ERROR` or `CRITICAL`. Raising it drops the record the server writes for every request it serves, which is worth doing when the server runs alongside a caller that logs those requests itself.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Model Context Protocol (MCP) server for Code Ocean.
|
|
4
4
|
|
|
5
|
-
This MCP server provides tools to search and
|
|
5
|
+
This MCP server provides tools to search, run, and release capsules and pipelines, and manage data assets.
|
|
6
6
|
|
|
7
7
|
## Table of Contents
|
|
8
8
|
|
|
@@ -230,3 +230,5 @@ This will start a web server where you can:
|
|
|
230
230
|
The MCP server supports custom log formatting through the `LOG_FORMAT` environment variable. This allows you to control the format of log messages output by the server.
|
|
231
231
|
**Example Format Strings:** `"%(asctime)s %(levelname)s [%(name)s] %(message)s"`.
|
|
232
232
|
If `LOG_FORMAT` is not set, the server uses FastMCP's default logging configuration.
|
|
233
|
+
|
|
234
|
+
The `LOG_LEVEL` environment variable sets the level the server logs at: `DEBUG`, `INFO` (the default), `WARNING`, `ERROR` or `CRITICAL`. Raising it drops the record the server writes for every request it serves, which is worth doing when the server runs alongside a caller that logs those requests itself.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "codeocean-mcp-server"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.13.1"
|
|
4
4
|
authors = [{ name = "Code Ocean", email = "dev@codeocean.com" }]
|
|
5
5
|
description = "Code Ocean MCP Server"
|
|
6
6
|
readme = "README.md"
|
|
@@ -12,8 +12,8 @@ classifiers = [
|
|
|
12
12
|
]
|
|
13
13
|
dependencies = [
|
|
14
14
|
"anyio>=4.5",
|
|
15
|
-
"codeocean>=0.
|
|
16
|
-
"mcp>=1.
|
|
15
|
+
"codeocean>=0.17.0,<0.18.0",
|
|
16
|
+
"mcp>=1.29.1,<1.30.0",
|
|
17
17
|
]
|
|
18
18
|
license = "MIT"
|
|
19
19
|
|
|
@@ -22,6 +22,7 @@ dev = [
|
|
|
22
22
|
"boto3>=1.42.17",
|
|
23
23
|
"deepdiff>=8.6.1",
|
|
24
24
|
"hatch>=1.16.2",
|
|
25
|
+
"httpx>=0.28.1",
|
|
25
26
|
"mcp-python-client>=0.1.9",
|
|
26
27
|
"pytest>=9.0.2",
|
|
27
28
|
"ruff>=0.14.10",
|
|
@@ -36,7 +37,7 @@ Changelog = "https://github.com/codeocean/codeocean-mcp-server/blob/main/CHANGEL
|
|
|
36
37
|
codeocean-mcp-server = "codeocean_mcp_server.server:main"
|
|
37
38
|
|
|
38
39
|
[build-system]
|
|
39
|
-
requires = ["hatchling"]
|
|
40
|
+
requires = ["hatchling!=1.32.1"]
|
|
40
41
|
build-backend = "hatchling.build"
|
|
41
42
|
|
|
42
43
|
[tool.hatch.build.targets.sdist]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def log_level() -> str:
|
|
7
|
+
"""Return the level to log at, from the LOG_LEVEL environment variable.
|
|
8
|
+
|
|
9
|
+
FastMCP has to be constructed with it as well: it configures uvicorn's loggers from its own level,
|
|
10
|
+
which it takes as a constructor argument that wins over its FASTMCP_LOG_LEVEL environment variable.
|
|
11
|
+
|
|
12
|
+
Environment variables:
|
|
13
|
+
LOG_LEVEL: DEBUG, INFO, WARNING, ERROR or CRITICAL (optional, defaults to INFO)
|
|
14
|
+
"""
|
|
15
|
+
return os.getenv("LOG_LEVEL", "").strip().upper() or "INFO"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def configure_logging(transport: str = "stdio") -> None:
|
|
19
|
+
"""Configure the root logger from the LOG_LEVEL and LOG_FORMAT environment variables.
|
|
20
|
+
|
|
21
|
+
LOG_LEVEL sets the root logger's level. LOG_FORMAT gives the root logger a stderr handler
|
|
22
|
+
with that format and, for a transport uvicorn serves, applies the same format to uvicorn's
|
|
23
|
+
own loggers. With neither set, logging is left as it is, for whoever embeds the server, or
|
|
24
|
+
else FastMCP with its defaults, to configure. Must be called before FastMCP is constructed.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
transport: The transport the server is about to serve on.
|
|
28
|
+
|
|
29
|
+
Environment variables:
|
|
30
|
+
LOG_LEVEL: DEBUG, INFO, WARNING, ERROR or CRITICAL (optional, defaults to INFO)
|
|
31
|
+
LOG_FORMAT: Python logging format string (optional)
|
|
32
|
+
|
|
33
|
+
Examples:
|
|
34
|
+
- "%(asctime)s agent %(levelname)s [%(name)s] %(message)s"
|
|
35
|
+
- "%(levelname)s: %(message)s"
|
|
36
|
+
- "[%(name)s] %(message)s"
|
|
37
|
+
|
|
38
|
+
Note:
|
|
39
|
+
A malformed format string fails here, at startup. One that names a field records do not
|
|
40
|
+
have fails with a ValueError when the first record is formatted instead.
|
|
41
|
+
|
|
42
|
+
"""
|
|
43
|
+
log_format = os.getenv("LOG_FORMAT", "").strip()
|
|
44
|
+
|
|
45
|
+
# The level is applied when either variable is set: a handler of our own leaves FastMCP's own
|
|
46
|
+
# logging.basicConfig() with nothing to do, so the level would otherwise stay at the default.
|
|
47
|
+
if log_format or "LOG_LEVEL" in os.environ:
|
|
48
|
+
logging.root.setLevel(log_level())
|
|
49
|
+
|
|
50
|
+
if not log_format:
|
|
51
|
+
return
|
|
52
|
+
|
|
53
|
+
handler = logging.StreamHandler(sys.stderr)
|
|
54
|
+
handler.setFormatter(logging.Formatter(log_format))
|
|
55
|
+
logging.root.addHandler(handler)
|
|
56
|
+
|
|
57
|
+
if transport == "stdio":
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
# Serving over HTTP, uvicorn logs through its own loggers, which it gives handlers that do not
|
|
61
|
+
# propagate to the root logger. It configures them from this dictionary, which FastMCP leaves
|
|
62
|
+
# at its default value, so its contents have to be replaced to be reached at all. The import
|
|
63
|
+
# is local because uvicorn, like in FastMCP itself, is only needed for the transports it serves.
|
|
64
|
+
from uvicorn.config import LOGGING_CONFIG
|
|
65
|
+
|
|
66
|
+
LOGGING_CONFIG["formatters"]["default"]["fmt"] = log_format
|
|
67
|
+
# Access records carry the request in fields of their own rather than in the message.
|
|
68
|
+
LOGGING_CONFIG["formatters"]["access"]["fmt"] = log_format.replace(
|
|
69
|
+
"%(message)s", '%(client_addr)s - "%(request_line)s" %(status_code)s'
|
|
70
|
+
)
|
{codeocean_mcp_server-0.12.2 → codeocean_mcp_server-0.13.1}/src/codeocean_mcp_server/server.py
RENAMED
|
@@ -9,7 +9,7 @@ from codeocean import CodeOcean
|
|
|
9
9
|
from mcp.server.fastmcp import FastMCP
|
|
10
10
|
|
|
11
11
|
from codeocean_mcp_server.client import RequestScopedClient
|
|
12
|
-
from codeocean_mcp_server.logging_config import configure_logging
|
|
12
|
+
from codeocean_mcp_server.logging_config import configure_logging, log_level
|
|
13
13
|
from codeocean_mcp_server.tools import (
|
|
14
14
|
capsules,
|
|
15
15
|
computations,
|
|
@@ -80,6 +80,7 @@ def main():
|
|
|
80
80
|
host=args.host,
|
|
81
81
|
port=args.port,
|
|
82
82
|
stateless_http=True,
|
|
83
|
+
log_level=log_level(),
|
|
83
84
|
)
|
|
84
85
|
|
|
85
86
|
if stdio:
|
|
@@ -2,11 +2,14 @@ from codeocean import CodeOcean
|
|
|
2
2
|
from codeocean.capsule import (
|
|
3
3
|
AppPanel,
|
|
4
4
|
Capsule,
|
|
5
|
+
CapsuleReleaseJob,
|
|
5
6
|
CapsuleSearchParams,
|
|
6
7
|
Computation,
|
|
7
8
|
DataAssetAttachParams,
|
|
8
9
|
DataAssetAttachResults,
|
|
10
|
+
GitSyncResults,
|
|
9
11
|
)
|
|
12
|
+
from codeocean.components import Permissions
|
|
10
13
|
from mcp.server.fastmcp import FastMCP
|
|
11
14
|
|
|
12
15
|
from codeocean_mcp_server.models import dataclass_to_pydantic
|
|
@@ -17,7 +20,7 @@ CapsuleSearchParamsModel = dataclass_to_pydantic(CapsuleSearchParams)
|
|
|
17
20
|
DataAssetAttachParamsModel = dataclass_to_pydantic(DataAssetAttachParams)
|
|
18
21
|
|
|
19
22
|
|
|
20
|
-
def add_tools(mcp: FastMCP, client: CodeOcean):
|
|
23
|
+
def add_tools(mcp: FastMCP, client: CodeOcean): # noqa: C901
|
|
21
24
|
"""Add capsule tools to the MCP server."""
|
|
22
25
|
|
|
23
26
|
@mcp.tool(description=(str(client.capsules.search_capsules.__doc__) + " " + str(CapsuleSearchResults.__doc__)))
|
|
@@ -86,3 +89,46 @@ def add_tools(mcp: FastMCP, client: CodeOcean):
|
|
|
86
89
|
def get_capsule_app_panel(capsule_id: str, version: int | None = None) -> AppPanelModel:
|
|
87
90
|
"""Retrieve the app panel for a capsule, optionally for a specific version."""
|
|
88
91
|
return client.capsules.get_capsule_app_panel(capsule_id, version)
|
|
92
|
+
|
|
93
|
+
@mcp.tool(description=(str(client.capsules.get_permissions.__doc__) + " Accepts a capsule ID or a pipeline ID."))
|
|
94
|
+
def get_capsule_permissions(capsule_id: str) -> Permissions:
|
|
95
|
+
"""Get the users, groups and everyone-role permissions of a capsule or pipeline."""
|
|
96
|
+
return client.capsules.get_permissions(capsule_id)
|
|
97
|
+
|
|
98
|
+
@mcp.tool(
|
|
99
|
+
description=(
|
|
100
|
+
str(client.capsules.sync_capsule.__doc__)
|
|
101
|
+
+ " Accepts a capsule ID or a pipeline ID. This pushes and pulls commits against the external"
|
|
102
|
+
" Git remote, so call it only when the user asked to sync."
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
def sync_capsule(capsule_id: str) -> GitSyncResults:
|
|
106
|
+
"""Sync a capsule or pipeline with its linked external Git repository."""
|
|
107
|
+
return client.capsules.sync_capsule(capsule_id)
|
|
108
|
+
|
|
109
|
+
@mcp.tool(description=(str(client.capsules.release_capsule.__doc__) + " Accepts a capsule ID or a pipeline ID."))
|
|
110
|
+
def release_capsule(capsule_id: str) -> CapsuleReleaseJob:
|
|
111
|
+
"""Start releasing a new version of an already-released capsule or pipeline."""
|
|
112
|
+
return client.capsules.release_capsule(capsule_id)
|
|
113
|
+
|
|
114
|
+
@mcp.tool(description=(str(client.capsules.get_release_job.__doc__) + " Accepts a capsule ID or a pipeline ID."))
|
|
115
|
+
def get_release_job(capsule_id: str, job_id: str) -> CapsuleReleaseJob:
|
|
116
|
+
"""Get the status of a capsule or pipeline release job."""
|
|
117
|
+
return client.capsules.get_release_job(capsule_id, job_id)
|
|
118
|
+
|
|
119
|
+
@mcp.tool(
|
|
120
|
+
description=(
|
|
121
|
+
str(client.capsules.wait_until_release_completed.__doc__)
|
|
122
|
+
+ " Accepts a capsule ID or a pipeline ID. Set `timeout` to bound the wait - without one this"
|
|
123
|
+
" polls until the release reaches a terminal state, however long that takes."
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
def wait_until_release_completed(
|
|
127
|
+
capsule_id: str,
|
|
128
|
+
job_id: str,
|
|
129
|
+
polling_interval: float = 5,
|
|
130
|
+
timeout: float | None = None,
|
|
131
|
+
) -> CapsuleReleaseJob:
|
|
132
|
+
"""Wait until a capsule or pipeline release job reaches a terminal state."""
|
|
133
|
+
job = client.capsules.get_release_job(capsule_id, job_id)
|
|
134
|
+
return client.capsules.wait_until_release_completed(capsule_id, job, polling_interval, timeout)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
|
|
3
3
|
from codeocean import CodeOcean
|
|
4
|
+
from codeocean.components import Permissions
|
|
4
5
|
from codeocean.data_asset import (
|
|
5
6
|
DataAsset,
|
|
6
7
|
DataAssetParams,
|
|
@@ -62,6 +63,11 @@ def add_tools(mcp: FastMCP, client: CodeOcean):
|
|
|
62
63
|
file_urls = client.data_assets.get_data_asset_file_urls(data_asset_id, file_path)
|
|
63
64
|
return download_and_read_file(file_urls.download_url)
|
|
64
65
|
|
|
66
|
+
@mcp.tool(description=client.data_assets.get_permissions.__doc__)
|
|
67
|
+
def get_data_asset_permissions(data_asset_id: str) -> Permissions:
|
|
68
|
+
"""Get the users, groups and everyone-role permissions of a data asset."""
|
|
69
|
+
return client.data_assets.get_permissions(data_asset_id)
|
|
70
|
+
|
|
65
71
|
@mcp.tool(description=client.data_assets.list_data_asset_files.__doc__)
|
|
66
72
|
def list_data_asset_files(data_asset_id: str, path: str = "") -> Folder:
|
|
67
73
|
"""List files in a data asset."""
|
|
@@ -16,24 +16,40 @@ import time
|
|
|
16
16
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
17
17
|
from pathlib import Path
|
|
18
18
|
|
|
19
|
+
import httpx
|
|
19
20
|
import pytest
|
|
20
21
|
from mcp import ClientSession, StdioServerParameters
|
|
21
|
-
from mcp.client.streamable_http import
|
|
22
|
+
from mcp.client.streamable_http import streamable_http_client
|
|
22
23
|
from mcp_client import get_tools
|
|
23
24
|
|
|
24
25
|
SERVER_SCRIPT_PATH = str(Path(__file__).parent.parent / "src" / "codeocean_mcp_server" / "server.py")
|
|
25
26
|
|
|
26
|
-
#
|
|
27
|
-
|
|
27
|
+
# Upper bound on the test's own waits, so a regression fails the test instead of hanging it.
|
|
28
|
+
RENDEZVOUS_TIMEOUT = 5.0
|
|
29
|
+
|
|
30
|
+
# The stub API's escape hatch from a release that never comes, far enough past the test's own waits
|
|
31
|
+
# that it cannot be what frees the server first.
|
|
32
|
+
HELD_REQUEST_TIMEOUT = 60.0
|
|
28
33
|
|
|
29
34
|
|
|
30
35
|
class _EchoTokenHandler(BaseHTTPRequestHandler):
|
|
31
|
-
"""Answer the custom metadata endpoint with the basic-auth user, which is the API token.
|
|
36
|
+
"""Answer the custom metadata endpoint with the basic-auth user, which is the API token.
|
|
37
|
+
|
|
38
|
+
A token named "slow-..." stands in for a tool that waits on Code Ocean: the handler reports that
|
|
39
|
+
the request has arrived, then holds the response until the test releases it. Only a released
|
|
40
|
+
request is answered, so the test cannot mistake a lapsed hold for a server that stayed free.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
slow_started = threading.Event()
|
|
44
|
+
slow_released = threading.Event()
|
|
32
45
|
|
|
33
46
|
def do_GET(self): # noqa: D102, N802
|
|
34
47
|
user = base64.b64decode(self.headers["Authorization"].split(" ")[1]).decode().split(":")[0]
|
|
35
48
|
if user.startswith("slow-"):
|
|
36
|
-
|
|
49
|
+
self.slow_started.set()
|
|
50
|
+
if not self.slow_released.wait(HELD_REQUEST_TIMEOUT):
|
|
51
|
+
self.send_error(500, "the held request was never released")
|
|
52
|
+
return
|
|
37
53
|
body = json.dumps({"categories": [user]}).encode()
|
|
38
54
|
self.send_response(200)
|
|
39
55
|
self.send_header("Content-Type", "application/json")
|
|
@@ -94,10 +110,11 @@ def _stdio_server(domain: str) -> StdioServerParameters:
|
|
|
94
110
|
|
|
95
111
|
async def _call_get_custom_metadata(url: str, token: str | None, scheme: str = "Bearer"):
|
|
96
112
|
headers = {"Authorization": f"{scheme} {token}"} if token else None
|
|
97
|
-
async with
|
|
98
|
-
async with
|
|
99
|
-
|
|
100
|
-
|
|
113
|
+
async with httpx.AsyncClient(headers=headers) as http_client:
|
|
114
|
+
async with streamable_http_client(url, http_client=http_client) as (read_stream, write_stream, _):
|
|
115
|
+
async with ClientSession(read_stream, write_stream) as session:
|
|
116
|
+
await session.initialize()
|
|
117
|
+
return await session.call_tool("get_custom_metadata", {})
|
|
101
118
|
|
|
102
119
|
|
|
103
120
|
def test_concurrent_requests_use_their_own_token(http_server):
|
|
@@ -116,20 +133,31 @@ def test_concurrent_requests_use_their_own_token(http_server):
|
|
|
116
133
|
|
|
117
134
|
|
|
118
135
|
def test_a_slow_tool_does_not_hold_up_other_requests(http_server):
|
|
119
|
-
"""A tool call still waiting on Code Ocean
|
|
120
|
-
url, _ = http_server
|
|
121
|
-
|
|
122
|
-
async def call_and_time(token: str):
|
|
123
|
-
result = await _call_get_custom_metadata(url, token)
|
|
124
|
-
return result, time.monotonic()
|
|
136
|
+
"""A second session is served while a tool call is still waiting on Code Ocean.
|
|
125
137
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
138
|
+
Fails if the synchronous tool runs on the event loop: the fast request then cannot even
|
|
139
|
+
initialize until the slow one is released, and times out.
|
|
140
|
+
"""
|
|
141
|
+
url, _ = http_server
|
|
142
|
+
handler = _EchoTokenHandler
|
|
143
|
+
|
|
144
|
+
async def fast_while_slow_is_blocked():
|
|
145
|
+
slow = asyncio.create_task(_call_get_custom_metadata(url, "slow-token"))
|
|
146
|
+
try:
|
|
147
|
+
assert await asyncio.to_thread(handler.slow_started.wait, RENDEZVOUS_TIMEOUT), (
|
|
148
|
+
"the slow tool call never reached the stub API"
|
|
149
|
+
)
|
|
150
|
+
try:
|
|
151
|
+
fast = await asyncio.wait_for(_call_get_custom_metadata(url, "fast-token"), RENDEZVOUS_TIMEOUT)
|
|
152
|
+
except asyncio.TimeoutError:
|
|
153
|
+
pytest.fail("the fast request did not complete while the slow tool call was blocked")
|
|
154
|
+
finally:
|
|
155
|
+
handler.slow_released.set()
|
|
156
|
+
return await slow, fast
|
|
157
|
+
|
|
158
|
+
slow, fast = asyncio.run(fast_while_slow_is_blocked())
|
|
130
159
|
assert slow.structuredContent["categories"] == ["slow-token"]
|
|
131
160
|
assert fast.structuredContent["categories"] == ["fast-token"]
|
|
132
|
-
assert fast_finished < slow_finished - 1
|
|
133
161
|
|
|
134
162
|
|
|
135
163
|
def test_request_without_credential_is_refused(http_server):
|
|
@@ -153,7 +181,7 @@ def test_tool_definitions_match_stdio(http_server):
|
|
|
153
181
|
url, domain = http_server
|
|
154
182
|
|
|
155
183
|
async def list_over_http():
|
|
156
|
-
async with
|
|
184
|
+
async with streamable_http_client(url) as (read_stream, write_stream, _):
|
|
157
185
|
async with ClientSession(read_stream, write_stream) as session:
|
|
158
186
|
await session.initialize()
|
|
159
187
|
return (await session.list_tools()).tools
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""End-to-end
|
|
1
|
+
"""End-to-end tests for LOG_LEVEL and LOG_FORMAT, over the transport that brings a logger of its own along."""
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
import re
|
|
@@ -30,9 +30,15 @@ def _wait_until_listening(port: int, timeout: float = 30.0):
|
|
|
30
30
|
raise TimeoutError(f"MCP server did not start listening on port {port}")
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
def _serve_and_request(
|
|
34
|
-
"""Serve over streamable HTTP
|
|
35
|
-
|
|
33
|
+
def _serve_and_request(**variables: str) -> str:
|
|
34
|
+
"""Serve over streamable HTTP with exactly these LOG_* variables set, request once, and return the log.
|
|
35
|
+
|
|
36
|
+
Whatever LOG_LEVEL and LOG_FORMAT the test run itself has are dropped, so that only the test
|
|
37
|
+
decides what the server logs.
|
|
38
|
+
"""
|
|
39
|
+
env = {name: value for name, value in os.environ.items() if name not in ("LOG_LEVEL", "LOG_FORMAT")}
|
|
40
|
+
env |= {"CODEOCEAN_DOMAIN": "test-domain", **variables}
|
|
41
|
+
port = _free_port()
|
|
36
42
|
process = subprocess.Popen(
|
|
37
43
|
[sys.executable, SERVER_SCRIPT_PATH, "--transport", "streamable-http", "--port", str(port)],
|
|
38
44
|
env=env,
|
|
@@ -55,7 +61,7 @@ def _serve_and_request(port: int) -> str:
|
|
|
55
61
|
|
|
56
62
|
def test_log_format_applies_to_uvicorns_own_records():
|
|
57
63
|
"""Uvicorn brings its own logging configuration, in whose format these records would be."""
|
|
58
|
-
log = _serve_and_request(
|
|
64
|
+
log = _serve_and_request(LOG_FORMAT=LOG_FORMAT)
|
|
59
65
|
|
|
60
66
|
timestamp = r"^[\d-]{10} [\d:,]+"
|
|
61
67
|
assert re.search(rf"{timestamp} server INFO \[uvicorn\.error\] Uvicorn running", log, re.MULTILINE)
|
|
@@ -66,3 +72,28 @@ def test_log_format_applies_to_uvicorns_own_records():
|
|
|
66
72
|
log,
|
|
67
73
|
re.MULTILINE,
|
|
68
74
|
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_log_level_quiets_a_request_the_caller_does_not_want_logged():
|
|
78
|
+
"""Both loggers have to be reached: uvicorn takes its level from FastMCP, the SDK from the root."""
|
|
79
|
+
log = _serve_and_request(LOG_LEVEL="WARNING", LOG_FORMAT=LOG_FORMAT)
|
|
80
|
+
|
|
81
|
+
assert "[uvicorn.access]" not in log
|
|
82
|
+
assert "[uvicorn.error]" not in log
|
|
83
|
+
assert "server INFO" not in log
|
|
84
|
+
# The rejected request is still warned about: the level drops the chatter, not the record
|
|
85
|
+
# that says something went wrong.
|
|
86
|
+
assert "server WARNING [" in log
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_log_level_applies_without_a_log_format():
|
|
90
|
+
"""Without LOG_FORMAT the records are FastMCP's own, and the level has to reach those too."""
|
|
91
|
+
noisy = _serve_and_request()
|
|
92
|
+
quiet = _serve_and_request(LOG_LEVEL="WARNING")
|
|
93
|
+
|
|
94
|
+
# FastMCP's rich-formatted records wrap, so match a word of each.
|
|
95
|
+
assert "Uvicorn running" in noisy
|
|
96
|
+
assert "streamable_http_manager" in noisy
|
|
97
|
+
assert "Uvicorn running" not in quiet
|
|
98
|
+
assert "streamable_http_manager" not in quiet
|
|
99
|
+
assert "Invalid Content-Type" in quiet # The rejected request's WARNING is still logged.
|