fix lint and rename
This commit is contained in:
parent
b9faec3f5f
commit
161b8da3e0
3 changed files with 5 additions and 5 deletions
|
|
@ -36,7 +36,7 @@ curl -X POST -H "Content-Type: application/json" -H "Accept: application/json, t
|
|||
http://localhost:3000/mcp
|
||||
```
|
||||
|
||||
### Client (`client/simpleStreamableHttpClient.ts`)
|
||||
### Client (`client/simpleStreamableHttp.ts`)
|
||||
|
||||
A client that connects to the server, initializes it, and demonstrates how to:
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ A client that connects to the server, initializes it, and demonstrates how to:
|
|||
#### Running the client
|
||||
|
||||
```bash
|
||||
npx tsx src/examples/client/simpleStreamableHttpClient.ts
|
||||
npx tsx src/examples/client/simpleStreamableHttp.ts
|
||||
```
|
||||
|
||||
Make sure the server is running before starting the client.
|
||||
|
|
|
|||
84
src/examples/client/simpleStreamableHttp.ts
Normal file
84
src/examples/client/simpleStreamableHttp.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import {
|
||||
ListToolsRequest,
|
||||
ListToolsResultSchema,
|
||||
CallToolRequest,
|
||||
CallToolResultSchema,
|
||||
ListPromptsRequest,
|
||||
ListPromptsResultSchema,
|
||||
GetPromptRequest,
|
||||
GetPromptResultSchema,
|
||||
ListResourcesRequest,
|
||||
ListResourcesResultSchema
|
||||
} from '../../types.js';
|
||||
|
||||
async function main(): Promise<void> {
|
||||
// Create a new client with streamable HTTP transport
|
||||
const client = new Client({
|
||||
name: 'example-client',
|
||||
version: '1.0.0'
|
||||
});
|
||||
const transport = new StreamableHTTPClientTransport(
|
||||
new URL('http://localhost:3000/mcp')
|
||||
);
|
||||
|
||||
// Connect the client using the transport and initialize the server
|
||||
await client.connect(transport);
|
||||
|
||||
console.log('Connected to MCP server');
|
||||
|
||||
// List available tools
|
||||
const toolsRequest: ListToolsRequest = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
|
||||
console.log('Available tools:', toolsResult.tools);
|
||||
|
||||
// Call the 'greet' tool
|
||||
const greetRequest: CallToolRequest = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'greet',
|
||||
arguments: { name: 'MCP User' }
|
||||
}
|
||||
};
|
||||
const greetResult = await client.request(greetRequest, CallToolResultSchema);
|
||||
console.log('Greeting result:', greetResult.content[0].text);
|
||||
|
||||
// List available prompts
|
||||
const promptsRequest: ListPromptsRequest = {
|
||||
method: 'prompts/list',
|
||||
params: {}
|
||||
};
|
||||
const promptsResult = await client.request(promptsRequest, ListPromptsResultSchema);
|
||||
console.log('Available prompts:', promptsResult.prompts);
|
||||
|
||||
// Get a prompt
|
||||
const promptRequest: GetPromptRequest = {
|
||||
method: 'prompts/get',
|
||||
params: {
|
||||
name: 'greeting-template',
|
||||
arguments: { name: 'MCP User' }
|
||||
}
|
||||
};
|
||||
const promptResult = await client.request(promptRequest, GetPromptResultSchema);
|
||||
console.log('Prompt template:', promptResult.messages[0].content.text);
|
||||
|
||||
// List available resources
|
||||
const resourcesRequest: ListResourcesRequest = {
|
||||
method: 'resources/list',
|
||||
params: {}
|
||||
};
|
||||
const resourcesResult = await client.request(resourcesRequest, ListResourcesResultSchema);
|
||||
console.log('Available resources:', resourcesResult.resources);
|
||||
|
||||
// Close the connection
|
||||
await client.close();
|
||||
}
|
||||
|
||||
main().catch((error: unknown) => {
|
||||
console.error('Error running MCP client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
@ -135,11 +135,11 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
|||
});
|
||||
|
||||
// Helper function to detect initialize requests
|
||||
function isInitializeRequest(body: any): boolean {
|
||||
function isInitializeRequest(body: unknown): boolean {
|
||||
if (Array.isArray(body)) {
|
||||
return body.some(msg => msg.method === 'initialize');
|
||||
return body.some(msg => typeof msg === 'object' && msg !== null && 'method' in msg && msg.method === 'initialize');
|
||||
}
|
||||
return body.method === 'initialize';
|
||||
return typeof body === 'object' && body !== null && 'method' in body && body.method === 'initialize';
|
||||
}
|
||||
|
||||
// Start the server
|
||||
|
|
|
|||
Loading…
Reference in a new issue