← Back

Connect Your Node.js Backend to Agent & LLM Analytics

Overview

This guide shows you how to use the NPM package to connect your website to Agent & LLM Analytics. Please contact us if you need help.

Step 1: Install the NPM Package

Download the package from NPM using the command line.

npm install @knownagents/sdk

Step 2: Initialize the Client

In your code, create an instance of KnownAgents with your project's access token.

import { KnownAgents } from "@knownagents/sdk"

const knownAgents = new KnownAgents("YOUR_ACCESS_TOKEN")

Step 3: Track Visits

Track Pageviews and REST Calls

Call trackPageviewOrRESTCall with the incoming request and outgoing response. If you can, do this in middleware.

knownAgents.trackPageviewOrRESTCall(request, response)

MCP calls are skipped automatically by trackPageviewOrRESTCall. Use trackMCPCall to track MCP calls.

High traffic websites should not use this method. Instead, batch visits and send them periodically with trackVisits. See the NPM page for an example.

Agentic Commerce Calls

For REST implementations of ACP and UCP, include the response body.

// For ACP calls ...

knownAgents.trackPageviewOrRESTCall(request, response, {
    acpResponseBody: acpResponseBody
})

// For UCP calls ...

knownAgents.trackPageviewOrRESTCall(request, response, {
    ucpResponseBody: ucpResponseBody
})

Track MCP Calls

For MCP servers that use StreamableHTTPServerTransport, call trackMCPCall after connecting the transport and before the transport handles the request.

High traffic websites, or those that use the same transport instance to handle multiple concurrent requests, should not use this method. Instead, batch visits and send them periodically with trackVisits. See the NPM page for an example.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"

// ...

const mcpServer = new McpServer({
    name: SERVER_NAME,
    version: SERVER_VERSION,
})

const transport = new StreamableHTTPServerTransport({
    sessionIdGenerator: undefined
})

// ...

await mcpServer.connect(transport)

knownAgents.trackMCPCall(request, response, transport)

await transport.handleRequest(request, response, parsedBody)

Agentic Commerce Calls

This method will track MCP implementations of ACP and UCP automatically.

Step 4: Test Your Integration

If your website is correctly connected, you should see visits from a test agent in the realtime timeline within a few seconds.