> ## Documentation Index
> Fetch the complete documentation index at: https://docs.atxp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# @atxp/server

> Documentation for the @atxp/server package

## Overview

The [@atxp/server](https://www.npmjs.com/package/@atxp/server) package is a support package used for packages like [@atxp/express](/developers/api-reference/express). You should generally not need to reference **@atxp/server** directly.

<Info>
  This package depends on types and utilities from [@atxp/common](/developers/api-reference/common). All shared types like `ATXPArgs` and payment utilities are documented in the common package.
</Info>

## Installation

```bash theme={null}
npm install @atxp/server
```

## Functions

### atxpAccountId

```typescript theme={null}
atxpAccountId(): string | null
```

Returns the accountId of the ATXP user of the current server request. Use this in the code of an MCP tool, or anywhere else inside the context of ATXP middleware, to get the current user.

<Note>
  Account ids should be treated as opaque strings. They are unique per user, but you should not assume they have any semantic meaning.
</Note>

**Arguments**
None

**Example usage**

```typescript theme={null}
import { requirePayment, atxpAccountId } from '@atxp/server'
import BigNumber from 'bignumber.js'

server.tool(
  "add",
  "Use this tool to return a personalized greeting.",
  {
    name: z.string().describe("Name of the user"),
  },
  async ({ name }) => {
    // Require payment for the tool call
    await requirePayment({price: BigNumber(0.01)});
    const accountId = atxpAccountId();
    return {
      content: [
        {
          type: "text",
          text: `Hello ${name}! Your user id is ${accountId}`
        },
      ],
    };
  }
);
```

### requirePayment

```typescript theme={null}
requirePayment({
  price: BigNumber,
  getExistingPaymentId?: () => Promise<string | null>
  }): Promise<void>
```

Requires payment before executing a tool. This function must be called before any paid tool logic.

<Note>
  The `requirePayment` function uses BigNumber for precise decimal arithmetic. Install it with `npm install bignumber.js` and import it as shown above.
</Note>

**Arguments**

<Expandable title="requirePayment arguments">
  <ParamField body="price" type="BigNumber" required>
    The price to charge in USDC. See [BigNumber](https://mikemcl.github.io/bignumber.js/) for BigNumber usage details.
  </ParamField>

  <ParamField body="getExistingPaymentId" type="function" optional>
    A function that returns the ID of an existing payment for the tool call. If not provided, a new payment will be created.
  </ParamField>
</Expandable>

**Example usage**

```typescript theme={null}
import { requirePayment } from '@atxp/server'
import BigNumber from 'bignumber.js'

server.tool(
  "add",
  "Use this tool to add two numbers together.",
  {
    a: z.number().describe("The first number to add"),
    b: z.number().describe("The second number to add"),
  },
  async ({ a, b }) => {
    // Require payment for the tool call
    await requirePayment({price: BigNumber(0.01)});
    return {
      content: [
        {
          type: "text",
          text: `${a + b}`,
        },
      ],
    };
  }
);
```
