import argparse
import json
import os
from pathlib import Path
import subprocess
import sys
from tempfile import TemporaryDirectory
def exercise(exe):
server=Path(__file__).with_name("station_weather.py")
args=[sys.executable,"-B",str(server)]
with TemporaryDirectory(prefix="mbsky-mcp-registration-") as temp:
root=Path(temp)
profile,one,two=[root/k for k in ("profile","workspace-one","workspace-two")]
for directory in (profile,one,two):
directory.mkdir()
env={k:os.environ[k] for k in ("SYSTEMROOT","WINDIR","PATH","TEMP","TMP","COMSPEC") if k in os.environ}
env.update(CLAUDE_CONFIG_DIR=str(profile),CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC="1",
DISABLE_AUTOUPDATER="1",DISABLE_TELEMETRY="1",DISABLE_ERROR_REPORTING="1",PYTHONUTF8="1",
HTTP_PROXY="http://127.0.0.1:1",HTTPS_PROXY="http://127.0.0.1:1")
def cli(*command,cwd=one):
result=subprocess.run([str(exe),"mcp",*command],cwd=cwd,env=env,input=b"",
capture_output=True,timeout=14)
return result.returncode,result.stdout.decode("utf-8"),result.stderr.decode("utf-8")
names={"local":"station-local","project":"station-team","user":"station-user"}
add=[]
for scope,name in names.items():
code,stdout,_=cli("add","--scope",scope,"--transport","stdio",name,"--",*args)
add.append([scope,code,f"Added stdio MCP server {name}" in stdout])
code,team,_=cli("get","station-team")
info=[code,"Scope: Project config" in team,"Pending approval" in team,"Type: stdio" in team]
config=json.loads((one/".mcp.json").read_bytes())["mcpServers"]
team_config=config["station-team"]
file_checked=[list(config),team_config["type"],team_config["command"]==args[0],team_config["args"]==args[1:]]
def states(directory):
code,output,_=cli("list",cwd=directory)
assert code==0
rows={}
for line in output.splitlines():
name,_,state=line.partition(": ")
if name in names.values():
rows[name]="pending" if "Pending approval" in state else ("connected" if "Connected" in state else state)
return rows
scope_results=[states(one),states(two)]
code,stdout,_=cli("remove","station-local","--scope","local")
removed=cli("get","station-local")
removal=[code,"Removed MCP server station-local" in stdout,removed[0],"station-local" in removed[2]]
cases=[
("initialize",{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"mbsky-check","version":"1.0.0"}}),
("notifications/initialized",{}),
("tools/list",{}),
("tools/call",{"name":"sample_weather","arguments":{"city":"beijing"}}),
("tools/call",{"name":"sample_weather","arguments":{"city":"unknown"}}),
("resources/list",{}),
("resources/read",{"uri":"weather://samples/cities"}),
]
messages=[]
for i,(method,params) in enumerate(cases):
item=dict(jsonrpc="2.0",method=method,params=params)
if i!=1:
item["id"]=i
messages.append(json.dumps(item))
process=subprocess.run(args,cwd=one,env=env,input=("\n".join(messages)+"\n").encode(),
capture_output=True,timeout=8,check=True)
assert not process.stderr,process.stderr
rpc={x["id"]:x["result"] for raw in process.stdout.splitlines() if (x:=json.loads(raw))}
rpc_results={"version":rpc[0]["protocolVersion"],"tools":[t["name"] for t in rpc[2]["tools"]],
"known":rpc[3]["content"][0]["text"],"unknown":rpc[4]["isError"],"uri":rpc[5]["resources"][0]["uri"],
"resource_text":rpc[6]["contents"][0]["text"]}
return dict(add=add,team=info,config=file_checked,scope=scope_results,removal=removal,rpc=rpc_results)
if __name__=="__main__":
p=argparse.ArgumentParser()
p.add_argument("claude",type=Path)
args=p.parse_args()
print(json.dumps(exercise(args.claude.resolve(strict=True))))
本例需在 Windows、Python 3.12、Claude Code 2.1.325,临时配置信息。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12、Claude Code 2.1.325,临时配置信息。
入口文件:projects/claude-code-mcp/mcp_local.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
mcp_local.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12、Claude Code 2.1.325,临时配置信息。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a3117.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3133.zip