#30 AI agents and MCP 5.2 min
MCP Over HTTP: Remote Servers and a Token Check
An MCP server on a network with no check answers anyone who can reach it, so I added a token check.
Run the token demo
Copy the code folder first. With the mcp package from the video installed, run python run.py. It starts the server, runs the client and stops the server. Check you see a 401 for the raw request, a refusal for no token and a wrong token, and a success for Ana's token that says Sunday is closed.
A hint
Everything runs on the loopback address for a few seconds.
Use the other known token
In client.py change ana-token in the tries list to ben-token and the label to ben's token. Predict what the last line prints, then run python run.py. Change the token and the label back to ana-token and ana's token afterwards, because the next exercise looks at Ana's token. This is a prediction task: we did not run the server here.
A hint
Look at the TOKENS dictionary at the top of server.py.
Take a token away
In server.py remove ana-token from the TOKENS dictionary. Predict what the last line of the output says now for Ana's token, then run python run.py. Put the entry back afterwards. This is a prediction task: we did not run the server here.
A hint
The verifier returns nothing for a token that is not in the dictionary.
Ask about another day
In client.py change Sunday to Saturday in the tool call, and make the printed line show the day. Predict what the known token session reports, then run python run.py. This is a prediction task: we did not run the server here. Then explain in one sentence why the day has nothing to do with the token check.
A hint
Read the opening_hours function in server.py. It only returns closed for Sunday.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
The tokens are two made-up fixed strings that never expire. There is no authorisation server, no OAuth flow, no audience check, no HTTPS, and no test of scopes or of the Origin header check. Everything ran on the loopback address of one laptop.
Up next in AI agents and MCP
More in AI agents and MCP
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
