stamp-mcp
An MCP server for NTP time and clock drift. Zero dependencies, fully synchronous, raw JSON-RPC 2.0 over stdio -- no MCP library, no asyncio.
A single NTP query tells you where your clock is right now. Drift history
tells you where it is going: a clock that is consistently 200ms fast and
accelerating is a different problem than one that is stable at 200ms fast.
get_time takes the measurement; every call appends to a local log;
get_drift reads the log and reports the trend.
Install
pip install stamp-mcp
Use with Claude Desktop
Edit ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"stamp": {
"command": "stamp-mcp"
}
}
}
Or run the module directly:
{
"mcpServers": {
"stamp": {
"command": "python3",
"args": ["-m", "stamp_mcp"]
}
}
}
Tools
get_time-- one NTP query: UTC time, this clock's offset in ms, network delay, stratum. Appends the sample to the drift log. Optional argument:server(defaulttime.cloudflare.com).get_drift-- analyzes the drift log: sample count, timespan, current/mean/stddev offset, drift rate in ms/day (least-squares fit), first-half vs. second-half rates, and a verdict:stable,drifting, oraccelerating. Optional argument:serverto filter samples.
The drift log
Every get_time call appends one JSON line to ~/.stamp/drift.jsonl
(override with STAMP_DRIFT_LOG). The file is capped at 10,000 samples.
Call get_time periodically -- a cron job, a heartbeat, or just asking
Claude "check the clock" now and then -- and get_drift turns the
accumulated offsets into a trend.
How it works
The whole server is stamp_mcp/server.py:
- JSON-RPC 2.0 over stdio --
initialize,tools/list,tools/call,ping, notifications, and the standard error codes (-32700,-32601). - Raw NTP with real offset math -- a 48-byte NTPv3 packet over UDP 123
carrying our transmit timestamp; the response's receive (t2) and
transmit (t3) timestamps are unpacked with
struct.unpack("!II", ...)as 64-bit fixed point, and offset/delay follow RFC 5905:offset = ((t2-t1)+(t3-t4))/2,delay = (t4-t1)-(t3-t2). - Two rules that matter: stdout is the protocol channel (log to stderr
only), and
flush()after every write (subprocess stdout is block-buffered).
Development
client.py is a test harness that plays the role of an MCP host -- it
spawns the server and performs the real handshake, printing every raw
frame:
python3 client.py # tests server.py (stage 1)
python3 client.py server_atomic.py # tests the standalone artifact
python3 client.py stamp_mcp/server.py # tests the package
server.py and server_atomic.py are the from-scratch learning artifacts;
stamp_mcp/ is the packaged, published server.
Publishing
The GitHub Action in .github/workflows/publish-mcp.yml runs on version
tags (git tag v0.3.0 && git push origin v0.3.0) and does two things:
- Publishes the package to PyPI -- requires a
PYPI_API_TOKENrepository secret (or configure Trusted Publishing on PyPI and remove thepasswordline). - Publishes metadata to the MCP Registry -- uses
mcp-publisherwith GitHub OIDC (id-token: write), no secret needed. The server nameio.github.theoddden/stampis bound to the GitHub account; themcp-nameHTML comment at the top of this README is the PyPI ownership verification marker.
Appendix: how this was built, stage by stage
Build an MCP server with no library, one concept at a time. By the end you will have written every line yourself and the official MCP SDK becomes a convenience you could discard.
Stage 1 -- raw JSON-RPC over stdio (DONE, verified)
server.py-- the entire protocol in ~100 lines:sys.stdin->json-> dispatch ->sys.stdout->flush().client.py-- plays the role of Claude Desktop. Spawns the server and performs the real handshake, printing every raw frame.
Run it:
python3 client.py
Things to notice in the output:
initializereturnsprotocolVersion,capabilities,serverInfo.notifications/initializedhas noidand gets no response.tools/listreturns the manifest;inputSchemais plain JSON Schema.tools/callresults are{"content": [{"type": "text", ...}]}.- Unknown method -> JSON-RPC error
-32601. - Unknown tool -> a normal result with
isError: true(so the model can read the failure and recover). - Malformed JSON ->
-32700.
Two rules that will bite you if ignored:
- stdout is the protocol channel. One stray
print()corrupts the stream. Log to stderr only. - flush() after every write. As a subprocess, stdout is block-buffered; without flush the host thinks the server is dead.
Stage 2 -- asyncio
Rewrite the stdin loop as an async coroutine:
async def main()+asyncio.run(main())- Read stdin without blocking the loop:
loop.run_in_executor(None, sys.stdin.readline)orasyncio.StreamReaderhooked to stdin vialoop.connect_read_pipe. awaiteach handler.
The payoff comes in stage 4 -- for now it is the same server with a different engine.
Stage 3 -- real NTP
Replace the stub get_time with a real query.
- First pass:
pip install ntplib, thenntplib.NTPClient().request('pool.ntp.org', version=3). - Second pass (optional, illuminating): delete ntplib and write the UDP
query yourself. NTPv3 packet = 48 bytes, first byte
0x1B(LI=0, VN=3, Mode=3), rest zeros. Send to port 123, read 48 bytes back, unpack the transmit timestamp (bytes 40-43, seconds since 1900) withstruct.unpack('!I', ...). Subtract 2208988800 to get Unix time. ntplib is ~200 lines of exactly this -- read its source once.
Stage 4 -- blocking vs. the event loop
The lesson you learn by breaking it:
- Call
ntplibdirectly inside yourasync defhandler. - While a slow NTP server is being queried, send a
pingfrom the client. Watch it hang -- the single-threaded event loop is frozen. - Fix it:
await loop.run_in_executor(None, blocking_ntp_call). Blocking work goes to the thread pool; async work gets awaited.
Rule of thumb: ntplib, requests, file I/O = blocking. aiohttp,
httpx (async mode), asyncpg = not blocking.
Connecting to Claude Desktop
Edit ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"ntp-scratch": {
"command": "/usr/bin/python3",
"args": ["/Users/theowolfenden/CascadeProjects/mcp-from-scratch/server.py"]
}
}
}
Restart Claude Desktop, then ask it "what tools do you have?" -- get_time
should appear. If it doesn't, check the logs at
~/Library/Logs/Claude/mcp*.log -- a stray print or missing flush is the
usual culprit.
The wire protocol, in one glance
>>> {"jsonrpc":"2.0","id":1,"method":"initialize","params":{...}}
<<< {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05",...}}
>>> {"jsonrpc":"2.0","method":"notifications/initialized"} (no reply)
>>> {"jsonrpc":"2.0","id":2,"method":"tools/list"}
<<< {"jsonrpc":"2.0","id":2,"result":{"tools":[...]}}
>>> {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_time","arguments":{}}}
<<< {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"..."}]}}
That is the whole thing. Everything else is plumbing.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stamp_mcp-0.3.0.tar.gz.
File metadata
- Download URL: stamp_mcp-0.3.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffb3aaf292428f5357c47ee302092d15324d2aeacaed4fc42faccf6035ec41a2
|
|
| MD5 |
d6bca61a1c5b6eb44942f3a9489b555d
|
|
| BLAKE2b-256 |
08d49e450b41a13ab192b1c347d84b84e99a125ed901358deb0ae07767508b48
|
File details
Details for the file stamp_mcp-0.3.0-py3-none-any.whl.
File metadata
- Download URL: stamp_mcp-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e5c068014fe29a8bf17f29f35b30c6e80e19792fbf80e9f477c819e0c323243
|
|
| MD5 |
bac85a2963b14b5de8a898132a16ec8c
|
|
| BLAKE2b-256 |
1cbf344909fdc9d5622b750e031c5c3497d881871f62c5b8bc915dc7a6555ace
|