forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.ts
More file actions
67 lines (62 loc) · 2.54 KB
/
Copy pathstdio.ts
File metadata and controls
67 lines (62 loc) · 2.54 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
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { findUserById } from '../auth/session.js'
import { config } from '../config.js'
import { readPortfolio } from '../connectors/portfolio/index.js'
import { getDb } from '../db/client.js'
import { SCOPES } from '../oauth/scopes.js'
import { portfolioProvider } from '../routes/portfolio-provider.js'
import { listWallets } from '../wallets/index.js'
import { buildServer, type ToolDeps } from './server.js'
/**
* The stdio variant, for local development. Same tool code, no OAuth.
*
* The MCP spec is explicit that stdio servers should not run the authorization
* flow and should take credentials from the environment instead — there is no
* browser to redirect and no origin to bind a grant to. So this trusts whoever
* launched the process, which is the same trust every stdio server operates
* under: the client already had to be able to spawn it.
*
* Remote is still the real surface. Web agent hosts cannot spawn a local
* process at all, so this exists to shorten the loop while writing tools, never
* as the way anybody connects.
*
* Anything written to stdout that is not a protocol message corrupts the
* stream, so diagnostics go to stderr. That is not a style preference here.
*/
async function main(): Promise<void> {
const userId = process.env.OTTOPUS_USER_ID
if (!userId) {
console.error('[mcp:stdio] set OTTOPUS_USER_ID to the account these tools should act for')
process.exit(1)
}
// The same reads the remote surface wires, against the same database: a
// tool that answered differently over stdio would be a tool nobody tested.
if (!config.databaseUrl) {
console.error('[mcp:stdio] set DATABASE_URL — the tools read wallets from it')
process.exit(1)
}
const db = getDb(config.databaseUrl)
const provider = portfolioProvider
const deps: ToolDeps = {
findUser: (id) => findUserById(db, id),
// No client row: nothing registered, because nothing was asked to.
findAgent: async () => ({ clientName: 'This local agent' }),
listWallets: (id) => listWallets(db, id),
readPortfolio: provider ? (arms) => readPortfolio(provider, arms) : null,
}
const server = buildServer(
{
userId,
clientId: 'stdio',
// No grant to narrow, because there was no consent screen to narrow it.
scopes: [...SCOPES],
},
deps,
)
await server.connect(new StdioServerTransport())
console.error('[mcp:stdio] ready')
}
main().catch((err: unknown) => {
console.error('[mcp:stdio]', err)
process.exit(1)
})