A Model Context Protocol server that provides greeting tools, resources, and prompts, demonstrating client-server interaction using TypeScript.
This project demonstrates the creation and interaction of a simple Model Context Protocol (MCP) server and a standalone MCP client using TypeScript and the @modelcontextprotocol/sdk
.
The setup includes:
my-mcp-greeter-server
: An MCP server that provides greeting-related tools, resources, and prompts.my-mcp-client-script
: A simple command-line client script that launches the server, connects to it, and interacts with its capabilities programmatically.Communication between the client and server in this example uses the stdio (standard input/output) transport mechanism.
This project was built following these main phases:
Before you begin, ensure you have the following installed:
npx
(usually included with npm) - useful for testing with MCP Inspector.my-mcp-greeter-server
)Project Setup:
my-mcp-greeter-server
.npm init -y
.npm install @modelcontextprotocol/sdk zod
.npm install -D typescript @types/node
.npx tsc --init
.tsconfig.json
(setting "module": "Node16"
, "target": "ES2022"
, "outDir": "./build"
, "rootDir": "./src"
, etc.).package.json
to include "type": "module"
and added build
/start
scripts.src/index.ts
.Server Implementation (src/index.ts
):
McpServer
, StdioServerTransport
, z
).name
and version
.McpServer
, passing the name, version, and declaring its capabilities (tools, resources, prompts).greet
): Used server.tool()
to create a function callable by clients. Included a description, defined input parameters with Zod (name
, politeness
), and implemented the handler to return a personalized greeting string.server-info
): Used server.resource()
to expose static data. Provided a unique URI (info://greeter/about
) and implemented the handler to return the server's name and version.suggest-greeting
): Used server.prompt()
to create a reusable interaction template. Included a description and implemented the handler to return a predefined set of user/assistant messages to guide an LLM interaction.StdioServerTransport
as the communication method.await server.connect(transport)
to make the server ready.console.error
statements for visibility during execution, especially important for stdio transport where stdout is used for protocol messages.Building & Fixing:
npm run build
to compile TypeScript to JavaScript in the build
directory.my-mcp-client-script
)Project Setup:
my-mcp-client-script
.npm init -y
.npm install @modelcontextprotocol/sdk
.npm install -D typescript @types/node
.tsconfig.json
similarly to the server project.package.json
with "type": "module"
and build
/start
scripts.src/client-script.ts
.Client Implementation (src/client-script.ts
):
Client
, StdioClientTransport
, path
, url
).index.js
file (relative or absolute).StdioClientTransport
, providing the command
(node
) and args
(the path to the server script). This configuration is key, as the client transport launches the server process.Client
instance, giving it an identity and declaring its intent to use tools and resources.await client.connect(transport)
, which launched the server process and established the MCP connection over its stdio streams.greet
tool using await client.callTool()
.server-info
resource using await client.readResource()
.suggest-greeting
prompt using await client.getPrompt()
.console.log
to display the responses received from the server.await client.close()
in a finally
block to cleanly shut down the connection and terminate the server process.Build Both Projects:
cd my-mcp-greeter-server && npm run build
cd ../my-mcp-client-script && npm run build
Run the Client:
cd my-mcp-client-script
npm run start
(or node build/client-script.js
)console.log
) and the server (console.error
), confirming successful communication and execution of tools/resources/prompts.The Server (GreeterServer
):
The Client (client-script.ts
):
While the client script tests the programmatic interaction, you can test the server's capabilities individually using the MCP Inspector:
# Make sure the server is NOT already running # Replace /path/to/... with the actual absolute path npx @modelcontextprotocol/inspector node /path/to/my-mcp-greeter-server/build/index.js
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!