---
description: "Tests a pathname against a route pattern."
---

# `matchPath`

Tests a pathname against a route pattern. It powers route-scoped tools: a tool's `match: "/orders"` (see [`defineWebTool`](/docs/reference/core/define-web-tool)) turns it on while its pattern matches, in [`createWebAgents`](/docs/reference/core/create-web-agents) and `<WebAgentsProvider>`. A `match` may be a pattern, several, or a RegExp; only the string patterns use `matchPath`. Call it yourself to test a pattern before you rely on it.

## Import

```ts
import { matchPath } from "@webmcp-agents/core";
```

## Example

```ts
import { matchPath } from "@webmcp-agents/core";

matchPath("/products/:slug", "/products/ocean-tee"); // true
matchPath("/products/:slug", "/products"); // false: needs a segment
matchPath("/search/*", "/search"); // true: * may be empty
matchPath("/search/*", "/search/results/2"); // true
matchPath("/checkout", "/checkout/"); // true: trailing slash
matchPath("/Checkout", "/checkout"); // false: case matters
matchPath("*", "/anything"); // true
```

## Options

`matchPath(pattern, pathname)`.

| Name | Type | Default | Description |
|---|---|---|---|
| `pattern` | `string` | required | Literal segments, `:param` (exactly one segment) and a trailing `*` (the rest, possibly empty), e.g. `"/products/:slug"`. `"*"` alone matches every path. |
| `pathname` | `string` | required | The path to test, e.g. `location.pathname`. A query string or hash is not part of it. |

## Returns

`true` if `pathname` matches `pattern`, otherwise `false`.

## Notes

- `*` only has meaning as the last segment; elsewhere it is a literal.
- Trailing slashes do not matter; case does.
- Compiled patterns are cached per pattern string.
- It is not `URLPattern`, which is not in every browser the polyfill targets.

## See also

- [Tools across your app](/docs/build/tools-across-your-app), the page that teaches `match`
- [`defineWebTool`](/docs/reference/core/define-web-tool)
- [`createWebAgents`](/docs/reference/core/create-web-agents)
- [`createToolRegistry`](/docs/reference/core/create-tool-registry)
