Skip to content

Commit dd88df6

Browse files
author
zho
committed
added examples
1 parent 2de2e5d commit dd88df6

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Examples
2+
3+
| File | Description |
4+
|------|-------------|
5+
| [custom-url-generator.ts](./custom-url-generator.ts) | Embed the MCP server as a library, register a **custom URL generator** for a namespace, and wire `PineconeClient` + `setupServer()`. |
6+
7+
Run with `npx tsx examples/custom-url-generator.ts` after `npm install` (requires valid Pinecone credentials in env).

examples/custom-url-generator.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Example: register a custom URL generator for your namespace.
3+
*
4+
* The Pinecone Read-Only MCP exposes a per-namespace URL registry so library
5+
* consumers can synthesize URLs from metadata when records do not already
6+
* carry a `url` field. Built-ins cover `mailing` (Boost archives) and
7+
* `slack-Cpplang`; everything else is up to you.
8+
*
9+
* Usage:
10+
* 1. Import { registerUrlGenerator, setupServer, ... } from the package.
11+
* 2. Call registerUrlGenerator(namespace, fn) BEFORE setupServer(config) so
12+
* the registry is populated when the server registers tools.
13+
* 3. The generate_urls tool, plus query/keyword_search/guided_query rows,
14+
* will pick up the generator automatically.
15+
*
16+
* Run from a project that depends on the package, or replace the import
17+
* with a relative path when running inside this repo.
18+
*/
19+
20+
import {
21+
PineconeClient,
22+
registerUrlGenerator,
23+
resolveConfig,
24+
setPineconeClient,
25+
setupServer,
26+
type UrlGenerationResult,
27+
} from '@will-cppa/pinecone-read-only-mcp';
28+
29+
// Example domain: a documentation index whose chunks carry { product, slug }
30+
// metadata. We turn that into https://docs.example.com/{product}/{slug}.
31+
registerUrlGenerator('product-docs', (metadata): UrlGenerationResult => {
32+
const product = typeof metadata['product'] === 'string' ? metadata['product'] : null;
33+
const slug = typeof metadata['slug'] === 'string' ? metadata['slug'] : null;
34+
if (!product || !slug) {
35+
return {
36+
url: null,
37+
method: 'unavailable',
38+
reason: 'product-docs requires both `product` and `slug` metadata fields',
39+
};
40+
}
41+
return {
42+
url: `https://docs.example.com/${product}/${slug}`,
43+
method: 'generated.custom',
44+
};
45+
});
46+
47+
async function main(): Promise<void> {
48+
const config = resolveConfig({ apiKey: process.env['PINECONE_API_KEY'] ?? 'demo-key' });
49+
setPineconeClient(
50+
new PineconeClient({
51+
apiKey: config.apiKey,
52+
indexName: config.indexName,
53+
sparseIndexName: config.sparseIndexName,
54+
rerankModel: config.rerankModel,
55+
requestTimeoutMs: config.requestTimeoutMs,
56+
})
57+
);
58+
59+
const server = await setupServer(config);
60+
// `server` is ready to connect to a transport. The generate_urls tool
61+
// (and any query result enrichment) will route `product-docs` records
62+
// through the generator registered above.
63+
void server;
64+
console.log('Custom URL generator registered for namespace "product-docs".');
65+
}
66+
67+
main().catch((err) => {
68+
console.error(err);
69+
process.exit(1);
70+
});

0 commit comments

Comments
 (0)