"""Offline VS Code .vscode/mcp.json validator & stdio HelloWorldServer roundtrip verifier for Ch25."""
import json
import subprocess
import sys
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code MCP config & stdio server verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: .vscode/mcp.json schema, VS Code >= 1.102 gate, and 4 server registration methods
mcp_json_text = """{
"servers": {
"HelloWorldServer": {
"type": "stdio",
"command": "python3",
"args": ["test.py"]
}
}
}"""
mcp_cfg = json.loads(mcp_json_text)
srv = mcp_cfg["servers"]["HelloWorldServer"]
vscode_version = (1, 102, 0)
config_methods = ["direct_install", "workspace_mcp_json", "user_settings", "chat.mcp.discovery.enabled"]
cases.append(
check(
"mcp_json_config_and_discovery",
"Verify .vscode/mcp.json stdio server schema, VS Code 1.102+ GA version check, and 4 setup modes",
{
"server_name": "HelloWorldServer",
"transport": "stdio",
"command_and_args": ["python3", ["test.py"]],
"mcp_ga_ready": True,
"config_method_count": 4,
},
{
"server_name": "HelloWorldServer",
"transport": srv["type"],
"command_and_args": [srv["command"], srv["args"]],
"mcp_ga_ready": vscode_version >= (1, 102, 0),
"config_method_count": len(config_methods),
},
)
)
# Case 2: Execute actual stdio test.py child process with MCP initialize JSON on stdin
test_py_src = (
"import sys\n"
"import json\n\n"
"_ = json.load(sys.stdin)\n"
'json.dump({"type": "text", "text": "Hello World from MCP!"}, sys.stdout)\n'
)
init_req = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}})
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "test.py"
script.write_text(test_py_src, encoding="utf-8")
proc = subprocess.run(
[sys.executable, "-I", str(script)],
input=init_req,
capture_output=True,
encoding="utf-8",
timeout=15,
)
resp = json.loads(proc.stdout)
cases.append(
check(
"stdio_mcp_helloworld_roundtrip",
"Verify local stdio MCP test.py reads JSON from stdin and responds with Hello World from MCP!",
{
"returncode": 0,
"response_type": "text",
"response_text": "Hello World from MCP!",
},
{
"returncode": proc.returncode,
"response_type": resp["type"],
"response_text": resp["text"],
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-mcp-servers-lab/explore-mcp-servers.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mcp-servers.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2215.zip