From “Build ANYTHING With an Advanced MCP Server (Python, Authentication, Databases & More)” by Tech With Tim
Building an authenticated MCP server in Python
Build an MCP server that knows who is calling it.
Terms
- MCP host — The software you use, such as Claude Desktop or Cursor, that can connect to an MCP server. 1:38
- MCP client — Runs inside the host and talks to one MCP server using the protocol. 3:18
- Transport — How the MCP server communicates: stdio, SSE or HTTP. 3:54
- Identity provider — A service that provides the identity for a user, such as Google or Stytch's own provider. 12:44
- Dynamic client registration — Lets an MCP client register itself with the authorization server automatically. 36:55
What you need
- Python 3.13 and uv — Used to create the project and install dependencies.
- ngrok account and auth token — Needed to create the public tunnel to localhost:8000.
- Stytch account and project — Provides the project ID, secret and domain stored in .env.
- Node.js and Claude Code — Used to run the Claude Code MCP host; Windows users need WSL.
Steps
- 1. Define tools and run over HTTP: Create the FastMCP app, add two @mcp.tool functions and run it with HTTP transport on 127.0.0.1:8000 with CORS middleware. 17:09
- 2. Expose the server with ngrok and add to: Run ngrok http 8000, then add the forwarding URL with /mcp using claude mcp add --transport http. 21:45
- 3. Create a Stytch project and set env values: Copy the project ID, secret and domain from the Stytch dashboard into a .env file. 26:41
- 4. Add the protected-resource metadata route: Add a custom route at /.well-known/oauth-protected-resource returning resource, authorization_servers, scopes and bearer methods. 34:44
- 5. Build the React login front end: Create a Vite React app, wrap the app in StytchProvider with a public token and render the login or identity provider component. 44:31
- 6. Tie notes to users via the database: Use the repository in the tools and read the sub claim from the JWT to filter notes by user. 61:05
What you need to build it
- Python 3.13 and uv — Used to create the project and install dependencies. 14:41
- ngrok account and auth token — Needed to create the public tunnel to localhost:8000. 21:26
- Stytch account and project — Provides the project ID, secret and domain stored in .env. 26:41
- Node.js and Claude Code — Used to run the Claude Code MCP host; Windows users need WSL. 22:15
Details on screen
- Stytch sign-in page — The Stytch homepage in a browser tab, with the 'Get started for free' button and a 'New' banner.
- ngrok auth token command — The ngrok Windows setup panel shows the command 'ngrok config add-authtoken' highlighted.
- Get notes per user — PyCharm shows get_my_notes reading the access token and calling NoteRepository.get_notes_by_user.
- SQLite engine string — database.py creates the engine with create_engine('sqlite:///database.db').
- Delegate option highlighted — Documentation highlights 'Delegate to an external authorization server' as option two.
Primitives control hierarchy
- : Prompts · : User-controlled · : Interactive templates invoked by user choice · : Slash commands, menu options
- : Resources · : Application-controlled · : Contextual data attached and managed by the client · : File contents, git history
- : Tools · : Model-controlled · : Functions exposed to the LLM to take actions · : API POST requests, file writing
Building an AI agent in pure PythonTech With Tim
Setting up Claude Code and building games with itTech With Tim
CSS Cross-Page Transitions and Shared Image AnimationCoding2GO
Six CSS features that replace JavaScriptCoding2GO