Build a Custom MCP Client

DEMANDBASE · DEVELOPER GUIDE

Build a Custom MCP Client

Connect your application or AI agent to Demandbase using OAuth 2.0 Dynamic Client Registration, Authorization Code + PKCE, and the Model Context Protocol.

Discover → Register → Authorize → Get token → Connect
OAuth 2.0 + PKCENo client secretMultiple redirect URIsStreamable HTTP

On this page

01

Before you begin

Make sure your application and Demandbase account are ready.

✓A Demandbase user account with access to an MCP-enabled tenant.
✓An MCP-compatible client supporting OAuth Authorization Code + PKCE (S256).
✓One or more HTTPS redirect URIs allowlisted by Demandbase.
✓Secure storage for registration metadata and OAuth tokens.
Redirect URI requirements

All redirect URIs must use HTTPS, be allowlisted by Demandbase, and match exactly during authorization and token exchange (including scheme, host, port, path, and trailing slash). Contact Demandbase Support for allowlisting.

02 Discover OAuth endpoints

Fetch the OAuth authorization server metadata. Use the endpoints returned by discovery rather than hardcoding them.

curl -sS 'https://gateway.demandbase.com/.well-known/oauth-authorization-server' \
  -H 'Accept: application/json'
What does discovery return?

The response includes registration_endpoint, authorization_endpoint, token_endpoint, issuer, scopes_supported, and code_challenge_methods_supported. In the verified configuration, registration is at https://gateway.demandbase.com/mcp/v1/register, and the authorization server is hosted at authentication.demandbase.com.

03 Register your client

Submit your application's name and HTTPS callbacks to the DCR endpoint. A single request may include multiple redirect URIs.

curl -sS -X POST 'https://gateway.demandbase.com/mcp/v1/register' \
  -H 'Content-Type: application/json' -H 'Accept: application/json' \
  -d '{
    "client_name": "My MCP Application",
    "redirect_uris": [
      "https://dev.example.com/oauth/callback",
      "https://app.example.com/oauth/callback",
      "https://eu.example.com/oauth/callback"
    ],
    "grant_types": ["authorization_code"],
    "response_types": ["code"],
    "token_endpoint_auth_method": "none"
  }'
Expected: HTTP 201 Created. The response includes a client_id and the accepted redirect_uris. The verified public-client flow does not issue or require a client secret. Save the returned client ID.
Example registration response
{
  "client_id": "0oaexampleclientid",
  "redirect_uris": [
    "https://dev.example.com/oauth/callback",
    "https://app.example.com/oauth/callback",
    "https://eu.example.com/oauth/callback"
  ],
  "grant_types": ["authorization_code"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}

04 Choose your redirect strategy

Option A · One registration

Include development, staging, production, and regional HTTPS callbacks in one redirect_uris array.

Option B · Separate registrations

Submit a DCR request for each environment, including only its callback URI(s).

Allowlisting is required for both. DCR does not automatically add redirect URIs to the authorization server's allowlist. Separate registration requests should not be assumed to create distinct underlying Okta applications or client IDs.

Each authorization request selects exactly one registered redirect_uri. Use that same URI for the token exchange.

05 Authorize the user with PKCE

Generate a unique verifier, S256 challenge, and state for each authorization attempt.

import base64, hashlib, secrets
verifier = secrets.token_urlsafe(64)
challenge = base64.urlsafe_b64encode(
    hashlib.sha256(verifier.encode('ascii')).digest()
).rstrip(b'=').decode('ascii')
state = secrets.token_urlsafe(24)

Open the discovered authorization_endpoint in the user's browser with these query parameters:

ParameterValue
response_typecode
client_idReturned by DCR
redirect_uriOne registered HTTPS callback
scopeopenid profile email
stateUnique random state
code_challengeGenerated challenge
code_challenge_methodS256

After login, the browser returns to the selected callback with code and state. Verify the state and reject OAuth errors before exchanging the code. Keep the verifier private.

06 Exchange the code for a token

Use the token_endpoint from discovery. The redirect URI must exactly match the authorization request.

curl -sS -X POST \
  'https://authentication.demandbase.com/oauth2/aus7fu7m8kDj90Z532p7/v1/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode 'client_id=YOUR_CLIENT_ID' \
  --data-urlencode 'code=YOUR_AUTHORIZATION_CODE' \
  --data-urlencode 'redirect_uri=https://app.example.com/oauth/callback' \
  --data-urlencode 'code_verifier=YOUR_ORIGINAL_VERIFIER'
Example token response
{
  "token_type": "Bearer",
  "expires_in": 86400,
  "access_token": "REDACTED",
  "scope": "email openid profile",
  "id_token": "REDACTED"
}
Token handling. Store tokens securely. Calculate expiration from the returned expires_in value (24 hours in the verified test). The tested flow did not return a refresh token; do not assume refresh-token support without verifying it.

07 Connect to the MCP server

Use the Bearer token with the MCP endpoint.

https://gateway.demandbase.com/mcp/servers/db-mcp

Use Authorization: Bearer YOUR_ACCESS_TOKEN, Content-Type: application/json, and Accept: application/json, text/event-stream with JSON-RPC POST requests.

initialize → notifications/initialized → tools/list → tools/call
1. Initialize
{
  "jsonrpc": "2.0", "id": 1, "method": "initialize",
  "params": {
    "protocolVersion": "2025-03-26",
    "capabilities": {},
    "clientInfo": {"name": "my-mcp-client", "version": "1.0.0"}
  }
}

The protocol version is illustrative; use a supported version and honor the negotiated response.

2. Complete initialization
{"jsonrpc":"2.0","method":"notifications/initialized"}

Send this notification after successful initialization.

3. List tools
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}

Use the returned names and input schemas.

4. Call a tool
{
  "jsonrpc": "2.0", "id": 3, "method": "tools/call",
  "params": {"name": "TOOL_NAME_FROM_TOOLS_LIST", "arguments": {}}
}
Sessions and response formats

Preserve Mcp-Session-Id if returned and send the negotiated MCP-Protocol-Version header when required. Handle JSON and SSE responses. Check JSON-RPC errors and tool-level isError; HTTP 200 alone does not guarantee tool success.

08 Troubleshooting

Registration and redirect errors

invalid_redirect_uri: verify exact registration and allowlisting. DCR HTTP 400: check JSON fields, grant types, and HTTPS callbacks. HTTP 401/403: contact Demandbase Support.

Authorization and token errors

invalid_grant: check expired/reused code, redirect mismatch, or incorrect PKCE verifier. Callback timeout: check hosted HTTPS callback routing. State mismatch: reject and restart authorization.

MCP and tool errors

HTTP 401: check token validity. HTTP 403: check user and tenant entitlements. Tool not found: refresh tools/list. Session errors: check session ID and negotiated protocol version.

Need help? Contact Demandbase Support or your account team. Include your client name, HTTPS redirect URIs, environment, HTTP status, and sanitized error response. Never send access tokens or authorization codes.

Demandbase MCP overview · MCP specification · OAuth DCR (RFC 7591)


Did this page help you?