mcpserver

command
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Notes MCP server

This example is an MCP server protected by mcpauth, with the tensile-auth OAuth provider that issues its tokens in the same binary. It shows the whole loop: an MCP client connects, you approve it on a consent page, it calls tools, every call lands in the audit log, and revoking the connection stops the next call.

The sign-in is a demo that signs in anyone by name. Don't deploy this server.

What it serves

Path What it is
/mcp The MCP endpoint, with the tools notes_list (scope notes:read) and notes_add (scope notes:write)
/.well-known/oauth-protected-resource/mcp Protected Resource Metadata (RFC 9728)
/.well-known/oauth-authorization-server Authorization server metadata (RFC 8414), also at /.well-known/openid-configuration
/login The demo sign-in
/consent The consent page
/connections Each connected client, with a button to revoke it
/audit Your consents, tool calls, and revocations

Connect Claude Code

  1. Start the server:

    go run ./examples/mcpserver
    

    It listens on localhost:8080 and uses http://localhost:8080 as its issuer. The provider accepts http only for localhost.

  2. Add the server to Claude Code:

    claude mcp add --transport http notes http://localhost:8080/mcp
    
  3. In Claude Code, run /mcp, choose notes, and choose to authenticate. Claude Code reads the Protected Resource Metadata from the 401 challenge, finds the authorization server, and identifies itself with its client ID metadata document, https://claude.ai/oauth/claude-code-client-metadata. The default CIMD allowlist admits claude.ai, so the server fetches that document and needs no registration.

  4. Your browser opens the sign-in page. Sign in with any name.

  5. The consent page shows:

    • the client's name, Claude Code, which is its own claim, and its verified host, claude.ai;
    • the host the approval goes to, localhost, with a warning, because any program on your computer can listen on a loopback address;
    • the resource, Notes at http://localhost:8080/mcp;
    • the scopes, notes:read and notes:write.

    Choose Allow. Every approval is recorded before the code is issued, and a loopback redirect never skips this page.

  6. Ask Claude Code to add a note and list your notes. It calls notes_add and notes_list with a token whose audience is http://localhost:8080/mcp. That token works nowhere else: not at another MCP server, and not at the provider's userinfo endpoint.

  7. Open http://localhost:8080/audit. Each tool call is a tool_called event with the tool, the decision, and a hash of the arguments, never the arguments themselves.

  8. Open http://localhost:8080/connections and choose Revoke. The next tool call from Claude Code gets 401 with error="invalid_token", at once, because the token is a session that the server checks on every request.

Run it behind your own login

Most applications already sign people in. Start the server with -own-login to play one:

go run ./examples/mcpserver -own-login

In this mode, /login is the application's own login. It sets its own session cookie, __Host-notes-app, and tensile-auth never starts a session from it directly. Instead, authhttp.Bridge wraps the provider's authorization endpoint, the consent page, and the other pages:

bridge, err := authhttp.Bridge(svc, authhttp.BridgeConfig{
	User: func(r *http.Request) (authhttp.AppUser, bool) {
		// Read the application's own session.
		return authhttp.AppUser{ID: user, AuthenticatedAt: signedIn, Scopes: Scopes}, ok
	},
})
provider.Register(mux, bridge)

On each request, the bridge asks the application who is signed in and keeps one short-lived tensile-auth session for that sign-in, in the __Host-bridge cookie. The connection flow is the same as in Connect Claude Code, with these differences:

  • The consent page appears exactly as before. The bridge never skips it.
  • Approving needs a sign-in to the application in the last 10 minutes, because the bridged session keeps the application's sign-in time. After that, the consent page sends you to /login again.
  • When someone else signs in on the same browser, or you choose Sign out on the home page, the bridge revokes the old session before anything else, so it can never approve for the wrong person.
  • The bridged session can approve connections, but it can't create personal access tokens or agents.

The browser client at /client works the same way in this mode.

Other clients

  • ChatGPT uses https://chatgpt.com/oauth/client.json, which the default allowlist admits. ChatGPT reaches the server only over public HTTPS, so run it behind a tunnel or a proxy and set -issuer to the public URL.
  • Codex uses https://chatgpt.com/oauth/codex/client.json, which the default allowlist admits, and returns to a loopback address on any port. Codex CLI 0.154.0 signed in to this server and was approved on its consent page on 4 December 2026.
  • Grok Build, formerly Cursor, uses dynamic client registration. Start the server with -register to turn it on.
  • Any other client can be configured in Config.Clients, as the test does.

Files

Name What it holds
main.go The server: the provider, the protector, the tools, and the pages
ownlogin.go The application's own login, which -own-login bridges to the provider with authhttp.Bridge
client.go, client.html, client.js The browser MCP client at /client, which -client serves, on by default

Test

go test ./examples/mcpserver/

TestConnectCallAuditRevoke runs steps 4 to 8 in process, with a configured client in place of Claude Code. TestOwnLoginBridgesTheConsent runs the same flow with -own-login, then signs in as another person and signs out.

Documentation

Overview

Command mcpserver is an MCP server protected by mcpauth, with the tensile-auth OAuth provider that issues its tokens in the same binary.

It serves a notes MCP server at /mcp with two tools, notes_list (scope notes:read) and notes_add (scope notes:write), a demo sign-in that signs in anyone by name, a consent page that shows everything the MCP specification asks for, a connections page that lists and revokes each connected client, and an audit page with every tool call.

Claude Code connects with its client ID metadata document, which the default CIMD allowlist admits:

go run ./examples/mcpserver
claude mcp add --transport http notes http://localhost:8080/mcp

With -own-login, the server plays an application that has its own login and session cookie: authhttp.Bridge turns its signed-in user into the session that the provider and the consent page use.

To add an MCP server to an application that serves its routes with authhttp.Mux, register the MCP path on a parent http.ServeMux beside the Mux, as this server registers /mcp beside its other routes:

root := http.NewServeMux()
protector.Register(root, "/mcp", server, nil)
root.Handle("/", mux) // the application's authhttp.Mux

Don't register /mcp on the Mux itself. The Mux authenticates every request first and refuses a token bound to the MCP resource with 401, unless authhttp.WithResource lists that resource, which also lets MCP tokens call the application's own routes.

The sign-in is a demo. Don't deploy this server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL