learning-it by example

#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.

1. Why does an MCP server on HTTP need an authorisation check when one on standard input and output does not in the same way?
2. In a Python MCP demo server over HTTP with a token check, what did the server return to a raw request that carried no token?
3. In MCP over HTTP, where does the client put its bearer token?
4. In a Python MCP over HTTP demo with a token check, a client tried three sessions: no token, a wrong token and Ana's known token. Which were refused and which worked?
5. In a Python MCP over HTTP demo, what is said about the tokens its server accepts?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All AI agents and MCP videos

Related videos