forked from cppalliance/pinecone-read-only-mcp-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-demo.ts
More file actions
134 lines (118 loc) · 4.09 KB
/
Copy pathmcp-demo.ts
File metadata and controls
134 lines (118 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Generic quickstart: embed setupCoreServer and call core MCP tools in-process.
*
* Flow: list_namespaces → count → query (preset fast, no rerank).
* Requires seeded data (seed-data.ts). Core resolveConfig disables the suggest-flow gate by default.
*/
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { config as loadEnv } from 'dotenv';
import {
createServer,
PineconeClient,
resolveConfig,
setupCoreServer,
} from '@will-cppa/pinecone-read-only-mcp';
import { exitOnDemoFailure } from '../lib/exit-on-failure.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createLinkedTransports } from '../mcp-linked-transport.js';
import { QUICKSTART_NAMESPACE } from './seed-data.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
loadEnv({ path: join(__dirname, '.env') });
loadEnv();
function parseToolJson(result: { content?: Array<{ type: string; text?: string }>; isError?: boolean }): unknown {
const text = result.content?.find((c) => c.type === 'text')?.text;
if (!text) {
throw new Error('Tool result had no text content');
}
return JSON.parse(text) as unknown;
}
async function main(): Promise<void> {
const apiKey = process.env['PINECONE_API_KEY']?.trim();
const indexName = process.env['PINECONE_INDEX_NAME']?.trim();
if (!apiKey || !indexName) {
console.log(
'[mcp-demo] Set PINECONE_API_KEY and PINECONE_INDEX_NAME (see examples/quickstart/.env.example). ' +
'Run seed-data.ts first, then this script.'
);
return;
}
const config = resolveConfig({
apiKey,
indexName,
});
const ctx = createServer(config);
ctx.setClient(
new PineconeClient({
apiKey: config.apiKey,
indexName: config.indexName,
sparseIndexName: config.sparseIndexName,
defaultTopK: config.defaultTopK,
requestTimeoutMs: config.requestTimeoutMs,
})
);
const { clientTransport, serverTransport } = createLinkedTransports();
const server = await setupCoreServer({ context: ctx });
await server.connect(serverTransport);
const client = new Client({ name: 'quickstart-demo', version: '1.0.0' });
await client.connect(clientTransport);
try {
console.log('\n--- list_namespaces ---');
const listRaw = await client.callTool({ name: 'list_namespaces', arguments: {} });
if (listRaw.isError) {
throw new Error(`list_namespaces failed: ${JSON.stringify(listRaw)}`);
}
const listPayload = parseToolJson(listRaw) as {
status?: string;
count?: number;
namespaces?: Array<{ name: string; record_count: number }>;
};
console.log(JSON.stringify(listPayload, null, 2));
const ns =
listPayload.namespaces?.find((n) => n.name === QUICKSTART_NAMESPACE)?.name ??
listPayload.namespaces?.[0]?.name;
if (!ns) {
throw new Error(
`No namespaces found. Run: npx tsx examples/quickstart/seed-data.ts (expected "${QUICKSTART_NAMESPACE}").`
);
}
console.log(`\n--- count (namespace="${ns}") ---`);
const countRaw = await client.callTool({
name: 'count',
arguments: {
namespace: ns,
query_text: 'document',
},
});
if (countRaw.isError) {
throw new Error(`count failed: ${JSON.stringify(countRaw)}`);
}
const countPayload = parseToolJson(countRaw);
console.log(JSON.stringify(countPayload, null, 2));
console.log(`\n--- query (namespace="${ns}", preset=fast) ---`);
const queryRaw = await client.callTool({
name: 'query',
arguments: {
namespace: ns,
query_text: 'functions and reusable logic',
preset: 'fast',
top_k: 3,
},
});
if (queryRaw.isError) {
throw new Error(`query failed: ${JSON.stringify(queryRaw)}`);
}
const queryPayload = parseToolJson(queryRaw);
console.log(JSON.stringify(queryPayload, null, 2));
console.log('\nQuickstart MCP demo completed successfully.');
} finally {
await client.close();
await server.close();
await ctx.teardown();
}
}
main()
.then(() => {
process.exit(0);
})
.catch(exitOnDemoFailure('mcp-demo'));