Skip to content

Commit ba364e1

Browse files
committed
unit test support for mcp notebook
Signed-off-by: Bryan Bednarski <[email protected]>
1 parent 2fef69d commit ba364e1

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

examples/notebooks/mcp_setup_and_integration.ipynb

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@
303303
" print(\" - Enter a branch name (e.g., 'develop', 'main') to clone and install\")\n",
304304
" print(\"=\" * 60)\n",
305305
"\n",
306-
" branch_name = input(\"\\nEnter your choice: \").strip()\n",
306+
" branch_name = os.getenv(\"NAT_NOTEBOOK_INSTALL_MODE\", \"\").strip()\n",
307+
" if not branch_name:\n",
308+
" branch_name = input(\"\\nEnter your choice: \").strip()\n",
307309
"\n",
308310
" # Default to 'main' if empty\n",
309311
" if not branch_name:\n",
@@ -499,22 +501,32 @@
499501
"id": "c8e1c22d",
500502
"metadata": {},
501503
"source": [
502-
"Wait about 10 seconds before running the next cell...\n",
503-
"\n",
504504
"The `--bg` command will run your server in the background of the notebook, so you might not see confirmation that the server is running. To see the startup logs, you can remove this flag and run the cell again. However, subsequent steps require the server to be run as a background process.\n",
505505
"\n",
506506
"Next, we will list the set of available tools from `simple_calculator` MCP server."
507507
]
508508
},
509+
{
510+
"cell_type": "code",
511+
"execution_count": null,
512+
"id": "c2d15d0d",
513+
"metadata": {},
514+
"outputs": [],
515+
"source": [
516+
"# Wait about 10 seconds before running the next cell...\n",
517+
"import time\n",
518+
"time.sleep(10)"
519+
]
520+
},
509521
{
510522
"cell_type": "code",
511523
"execution_count": null,
512524
"id": "afdef7a1",
513525
"metadata": {},
514526
"outputs": [],
515527
"source": [
516-
"# For streamable-http transport (default)\n",
517-
"!nat mcp client tool list --url http://localhost:9901/mcp"
528+
"%%bash\n",
529+
"nat mcp client tool list --url http://localhost:9901/mcp"
518530
]
519531
},
520532
{
@@ -572,11 +584,15 @@
572584
]
573585
},
574586
{
575-
"cell_type": "markdown",
576-
"id": "9b013a00",
587+
"cell_type": "code",
588+
"execution_count": null,
589+
"id": "111c81a7",
577590
"metadata": {},
591+
"outputs": [],
578592
"source": [
579-
"Wait about 10 seconds before running the next cell..."
593+
"# Wait about 10 seconds before running the next cell...\n",
594+
"import time\n",
595+
"time.sleep(10)"
580596
]
581597
},
582598
{
@@ -629,11 +645,15 @@
629645
]
630646
},
631647
{
632-
"cell_type": "markdown",
633-
"id": "664a9ed6",
648+
"cell_type": "code",
649+
"execution_count": null,
650+
"id": "7ce2eebb",
634651
"metadata": {},
652+
"outputs": [],
635653
"source": [
636-
"Wait about 10 seconds before running the next cell..."
654+
"# Wait about 10 seconds before running the next cell...\n",
655+
"import time\n",
656+
"time.sleep(10)"
637657
]
638658
},
639659
{
@@ -954,11 +974,15 @@
954974
]
955975
},
956976
{
957-
"cell_type": "markdown",
958-
"id": "feae920e",
977+
"cell_type": "code",
978+
"execution_count": null,
979+
"id": "b7efca58",
959980
"metadata": {},
981+
"outputs": [],
960982
"source": [
961-
"Wait about 10 seconds before running the next cell..."
983+
"# Wait about 10 seconds before running the next cell...\n",
984+
"import time\n",
985+
"time.sleep(10)"
962986
]
963987
},
964988
{
@@ -1071,11 +1095,15 @@
10711095
]
10721096
},
10731097
{
1074-
"cell_type": "markdown",
1075-
"id": "b5d33205",
1098+
"cell_type": "code",
1099+
"execution_count": null,
1100+
"id": "7dc57c53",
10761101
"metadata": {},
1102+
"outputs": [],
10771103
"source": [
1078-
"Wait about 10 seconds before running the next cell..."
1104+
"# Wait about 10 seconds before running the next cell...\n",
1105+
"import time\n",
1106+
"time.sleep(10)"
10791107
]
10801108
},
10811109
{
@@ -1119,11 +1147,15 @@
11191147
]
11201148
},
11211149
{
1122-
"cell_type": "markdown",
1123-
"id": "4835e115",
1150+
"cell_type": "code",
1151+
"execution_count": null,
1152+
"id": "cc1de7ca",
11241153
"metadata": {},
1154+
"outputs": [],
11251155
"source": [
1126-
"Wait about 10 seconds before running the next cell..."
1156+
"# Wait about 10 seconds before running the next cell...\n",
1157+
"import time\n",
1158+
"time.sleep(10)"
11271159
]
11281160
},
11291161
{

examples/notebooks/tests/test_notebooks_e2e.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
import shutil
1716
import subprocess
1817
from pathlib import Path
18+
import os
1919

2020
import pytest
2121

@@ -90,9 +90,6 @@ def _delete_other_files(notebooks_dir: Path):
9090
for file in _OTHER_FILES:
9191
file_path = notebooks_dir / file
9292
if file_path.exists():
93-
# if file_path.is_dir():
94-
# shutil.rmtree(file_path)
95-
# else:
9693
file_path.unlink()
9794

9895

@@ -118,9 +115,12 @@ def _run_notebook(notebook_path: Path, expected_packages: list[str], timeout_sec
118115
str(notebook_path.absolute()),
119116
]
120117

118+
env = os.environ.copy()
119+
env["NAT_NOTEBOOK_INSTALL_MODE"] = "local"
120+
121121
# Ideally if the notebook times out we want jupyter to catch it and exit gracefully with the most informative error
122122
# possible. However in the potential situation where jupyter itself hangs, we add a 10s buffer to the timeout.
123-
result = subprocess.run(cmd, check=False, capture_output=True, text=True, timeout=timeout_seconds + 10)
123+
result = subprocess.run(cmd, check=False, capture_output=True, text=True, timeout=timeout_seconds + 10, env=env)
124124
assert result.returncode == 0, f"Notebook execution failed:\n{result.stderr}"
125125

126126
for package in expected_packages:

0 commit comments

Comments
 (0)