| 14 | import { ListResourcesResultSchema } from '../src/types.js'; |
| 15 | |
| 16 | async function runClient(url_or_command: string, args: string[]) { |
| 17 | const client = new Client( |
| 18 | { |
| 19 | name: 'mcp-typescript test client', |
| 20 | version: '0.1.0' |
| 21 | }, |
| 22 | { |
| 23 | capabilities: { |
| 24 | sampling: {} |
| 25 | } |
| 26 | } |
| 27 | ); |
| 28 | |
| 29 | let clientTransport; |
| 30 | |
| 31 | let url: URL | undefined = undefined; |
| 32 | try { |
| 33 | url = new URL(url_or_command); |
| 34 | } catch { |
| 35 | // Ignore |
| 36 | } |
| 37 | |
| 38 | if (url?.protocol === 'http:' || url?.protocol === 'https:') { |
| 39 | clientTransport = new SSEClientTransport(new URL(url_or_command)); |
| 40 | } else if (url?.protocol === 'ws:' || url?.protocol === 'wss:') { |
| 41 | clientTransport = new WebSocketClientTransport(new URL(url_or_command)); |
| 42 | } else { |
| 43 | clientTransport = new StdioClientTransport({ |
| 44 | command: url_or_command, |
| 45 | args |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | console.log('Connected to server.'); |
| 50 | |
| 51 | await client.connect(clientTransport); |
| 52 | console.log('Initialized.'); |
| 53 | |
| 54 | await client.request({ method: 'resources/list' }, ListResourcesResultSchema); |
| 55 | |
| 56 | await client.close(); |
| 57 | console.log('Closed.'); |
| 58 | } |
| 59 | |
| 60 | async function runServer(port: number | null) { |
| 61 | if (port !== null) { |