# Cost Basis Lots MCP: FIFO, LIFO, HIFO, Average (`agustin-recoba/cost-basis-lots-mcp`) Actor

MCP server and Actor for cost-basis lot accounting: realised gain per lot under FIFO, LIFO, HIFO or average cost, short or long term, open lots and a method comparison. Exact decimals. Not tax advice.

- **URL**: https://apify.com/agustin-recoba/cost-basis-lots-mcp.md
- **Developed by:** [Agustín Recoba](https://apify.com/agustin-recoba) (community)
- **Categories:** Developer tools, Automation
- **Features:** Limited permissions, Multi-tenant, Server interface
- **Stats:** 1 total users, 0 monthly users, 0.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $10.00 / 1,000 ledger calculations

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Cost basis lots: MCP server for FIFO, LIFO, HIFO and average cost

Give it a ledger of buys, sells and splits in one currency. For every sale it returns which lots were used and the proceeds, cost basis, gain and short or long term, under FIFO, LIFO, HIFO or average cost. Arithmetic is exact decimal: no floating point and no language model.

It is a calculator, not tax, legal or accounting advice. It applies no tax rule other than a holding period you choose: no wash-sale, like-kind or jurisdiction rules and no tax forms. Check results with a qualified person before filing.

### Connect an AI agent (MCP)

The server speaks MCP over Streamable HTTP. Every request needs your own Apify API token.

```json
{
  "mcpServers": {
    "cost-basis-lots": {
      "url": "https://agustin-recoba--cost-basis-lots-mcp.apify.actor/mcp",
      "headers": { "Authorization": "Bearer <YOUR_APIFY_TOKEN>" }
    }
  }
}
```

| Tool | What it does | Charged |
| --- | --- | --- |
| `validate_ledger` | Every problem that would stop a calculation | no |
| `compute_gains` | Gain per lot used, totals and open lots. `method`: `fifo` (default), `lifo`, `hifo`, `average`; optional `tax_year`, `decimals`, `holding_days` | yes |
| `compare_methods` | The same ledger and year under all four methods | yes |
| `open_lots` | Quantity and remaining basis of each lot held, optionally `as_of` a date | yes |

Ask your agent, for example: "Which method gives the smallest gain for 2024 on this ledger?"

### Ledger format

```json
{"events": [
  {"type": "buy",  "asset": "BTC", "date": "2023-01-10", "quantity": "1",   "price": "10000", "fee": "10", "id": "jan"},
  {"type": "buy",  "asset": "BTC", "date": "2023-06-01", "quantity": "1",   "price": "30000", "id": "jun"},
  {"type": "sell", "asset": "BTC", "date": "2024-02-01", "quantity": "1.5", "price": "40000", "fee": "15"}
]}
```

- `type`: `buy`, `receive` (units that arrive without a purchase, `price` is their fair value per unit), `sell` or `split` (`ratio` 2 turns one unit into two).
- Give exactly one of `price` (per unit) or `amount` (total). Numbers or numeric strings.
- `fee` is added to the basis of a buy and taken off the proceeds of a sell.
- `id` names a lot. A sell may choose lots with `"lots": [{"id": "jan", "quantity": "1"}]`.
- Dates are `YYYY-MM-DD`. Events are replayed in date order.

### Run it without MCP

Pick an operation on the **Input** tab and start a run. The run stores one dataset item. The prefilled ledger is the made-up one above.

```json
{ "operation": "open_lots", "events": [
  {"type": "buy", "asset": "ABC", "date": "2024-01-02", "quantity": 10, "price": 5}
] }
```

The output item has `operation` and `result`; `result` is what the MCP tool returns.

### How results are computed

- FIFO takes the oldest lot first, LIFO the newest, HIFO the highest unit basis. `average` pools each asset at its moving average cost and reports the term as `n/a`.
- Long term means sold after the first anniversary of acquisition, or held more than `holding_days`.
- Each row is rounded half up to `decimals` (default 2). Totals add the rounded rows, so they can differ from the exact total by a few cents.

### Pricing

Pay per event. One successful `compute_gains`, `compare_methods` or `open_lots` call costs USD 0.01, through the MCP server or a run. `validate_ledger`, `tools/list` and a ledger that is refused are not charged. A run started on the Input tab also has the Actor start event (USD 0.00005).

### What it does not do

No CSV or exchange import, no transfers between accounts, no currency conversion, no income tax on received units, no wash-sale or other jurisdiction rules, no Form 8949 or 1099-DA.

### Privacy

The ledger is processed in memory. The server writes no part of it to disk or to the log and calls no other service with it. The one outbound call is the charge to the Apify API, which carries a request id and an event name. A run started on the Input tab stores your input and the result in your own Apify storage. Requests pass through Apify's platform. Try it with anonymised or made-up data.

### Limits

- Up to 5,000 events and 200 assets per call.
- The server accepts 600 tool calls a minute, shared by all callers of one instance. Over the limit it answers with the seconds to wait.
- The first call after a quiet period waits for an instance to start.
- MCP revisions: 2026-07-28 and the `initialize` based ones from 2024-11-05 to 2025-11-25.

### Support and authorship

Open an issue on the **Issues** tab. It was written with AI assistance and tested with hand-computed examples.

# Changelog

This Actor's version history is a separate document: https://apify.com/agustin-recoba/cost-basis-lots-mcp/changelog.md

# Actor input Schema

## `operation` (type: `string`):

What to do.

## `events` (type: `array`):

Up to 5,000 events in one currency: type (buy, receive, sell or split), asset, date (YYYY-MM-DD), quantity and price or amount, optional fee, id and lots. The README describes each field. The prefilled ledger is made up.

## `method` (type: `string`):

Used by realised gains.

## `taxYear` (type: `integer`):

Only report sales made in this calendar year (gains and comparison).

## `holdingDays` (type: `integer`):

Long term when held more than this many days. Empty: sold after the first anniversary of acquisition.

## `asOf` (type: `string`):

YYYY-MM-DD (open lots only).

## `decimals` (type: `integer`):

Decimal places of reported money.

## Actor input object example

```json
{
  "operation": "compute_gains",
  "events": [
    {
      "type": "buy",
      "asset": "BTC",
      "date": "2023-01-10",
      "quantity": "1",
      "price": "10000",
      "fee": "10",
      "id": "jan"
    },
    {
      "type": "buy",
      "asset": "BTC",
      "date": "2023-06-01",
      "quantity": "1",
      "price": "30000",
      "id": "jun"
    },
    {
      "type": "sell",
      "asset": "BTC",
      "date": "2024-02-01",
      "quantity": "1.5",
      "price": "40000",
      "fee": "15"
    }
  ],
  "method": "fifo",
  "decimals": 2
}
```

# Actor output Schema

## `result` (type: `string`):

One item with the result of the operation.

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "events": [
        {
            "type": "buy",
            "asset": "BTC",
            "date": "2023-01-10",
            "quantity": "1",
            "price": "10000",
            "fee": "10",
            "id": "jan"
        },
        {
            "type": "buy",
            "asset": "BTC",
            "date": "2023-06-01",
            "quantity": "1",
            "price": "30000",
            "id": "jun"
        },
        {
            "type": "sell",
            "asset": "BTC",
            "date": "2024-02-01",
            "quantity": "1.5",
            "price": "40000",
            "fee": "15"
        }
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("agustin-recoba/cost-basis-lots-mcp").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "events": [
        {
            "type": "buy",
            "asset": "BTC",
            "date": "2023-01-10",
            "quantity": "1",
            "price": "10000",
            "fee": "10",
            "id": "jan",
        },
        {
            "type": "buy",
            "asset": "BTC",
            "date": "2023-06-01",
            "quantity": "1",
            "price": "30000",
            "id": "jun",
        },
        {
            "type": "sell",
            "asset": "BTC",
            "date": "2024-02-01",
            "quantity": "1.5",
            "price": "40000",
            "fee": "15",
        },
    ] }

# Run the Actor and wait for it to finish
run = client.actor("agustin-recoba/cost-basis-lots-mcp").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "events": [
    {
      "type": "buy",
      "asset": "BTC",
      "date": "2023-01-10",
      "quantity": "1",
      "price": "10000",
      "fee": "10",
      "id": "jan"
    },
    {
      "type": "buy",
      "asset": "BTC",
      "date": "2023-06-01",
      "quantity": "1",
      "price": "30000",
      "id": "jun"
    },
    {
      "type": "sell",
      "asset": "BTC",
      "date": "2024-02-01",
      "quantity": "1.5",
      "price": "40000",
      "fee": "15"
    }
  ]
}' |
apify call agustin-recoba/cost-basis-lots-mcp --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,agustin-recoba/cost-basis-lots-mcp"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/MBgBfUOpTJmdfO5Km/builds/txAz7KMTPForOWXXZ/openapi.json
