Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Using TDD to Build MCP

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Introduction to Using TDD to Build MCP

This presentation is an introduction to using test driven development (TDD) to develop a solution for model context protocol (MCP) primarily using Java.

The content is suitable for beginner to intermediate learners of either TDD or MCP.

The presentation was originally delivered to a group of my peers in an academic context.

Avatar for Justin

Justin

May 07, 2025
Tweet

More Decks by Justin

Other Decks in Programming

Transcript

  1. J U S T I N B I A R

    D / C O M P - 6 4 5 Q 1 W W A P R I L 1 , 2 0 2 5 TDD: TEST DRIVEN DEVELOPMENT
  2. AGENDA • Context and requirements • Tools and libraries used

    • The process • Example solution • Conclusion
  3. CONTEXT We are here… MCP Tools[3] are to AI, as

    Remote Method Invocation (RMI) is to a Java program.
  4. REQUIREMENTS: MCP TOOLS ONLY • Requests and responses are delivered

    using JSON-RPC[1] formatted messages • For example, responses contain: • jsonrpc: the version (2.0) • result: optional • error: optional, unless there is an error • Id: a number or a string • MCP servers and clients collaborate to give Artificial Intelligence (AI) models access to retrieve specific context or to take specific actions (through tools) • For example, MCP tools define: • name: a short string name • arguments: the input schema • description: a text description of the tool JSON-RPC 2.0 MCP 2024-11-05:
  5. DEVELOPER TOOLS + LIBRARIES • IntelliJ + Java • For

    development of the MCP tools and unit tests • Maven (+Make) • For automation of the build and MCP inspection pipeline • Bun • For debugging via MCP inspector • Claude Desktop • For execution of the MCP server • JDK 21 • JUnit Jupiter (5.12.0) • For unit test development • AssertJ (3.25.3) • For fluent-style assertions • Jackson Databind (2.18.3) • For mapping JSON to/from plain old Java objects (POJO) Developer Tools Libraries
  6. TEST • Pick a requirement (“initialize server”[2]) and blocked out

    how we want the API to look • Identify placeholders for “Request” and “Response” which do not yet exist • Servers complying with this interface should return a protocol version, so we assert that value too
  7. TEST • Pick a requirement (“initialize server”[2]) and blocked out

    how we want the API to look • Identify placeholders for “Request” and “Response” which do not yet exist • Servers complying with this interface should return a protocol version, so we assert that value too CODE • Create the Server interface • Define the placeholder method signatures • Define the Request and Response classes (as empty classes at this point) • Update import statements in ServerTest to address compiler warnings
  8. TEST • Pick a requirement (“initialize server”[2]) and blocked out

    how we want the API to look • Identify placeholders for “Request” and “Response” which do not yet exist • Servers complying with this interface should return a protocol version, so we assert that value too CODE • Create the Server interface • Define the placeholder method signatures • Define the Request and Response classes (as empty classes at this point) • Update import statements in ServerTest to address compiler warnings REPEAT
  9. COMPLETED TEST @Test void testServerInterface() { // given var server

    = new Server() { @Override public Response initialize(Request request) { return null; } @Override public Response listTools(Request request) { return null; } @Override public Response invokeTool(Request request) { return null; } @Override public Response handleRequest(Request request) { return null; } }; // when -> then assertThat(server.getProtocolVersion()).isNotBlank(); }
  10. USING THE MCP TOOL IN CLAUDE[4] // claude_desktop_config.json { "mcpServers":

    { "tdd": { "command": "java", "args": [ "-jar", "path-to.jar" ] },… } }
  11. USING THE MCP TOOL IN CLAUDE[4] // claude_desktop_config.json { "mcpServers":

    { "tdd": { "command": "java", "args": [ "-jar", "path-to.jar" ] },… } }
  12. USING THE MCP TOOL IN CLAUDE[4] // claude_desktop_config.json { "mcpServers":

    { "tdd": { "command": "java", "args": [ "-jar", "path-to.jar" ] },… } }
  13. CONCLUSION What we saw: • MCP and MCP Tools as

    example requirements including the use of JSON-RPC
  14. CONCLUSION What we saw: • MCP and MCP Tools as

    example requirements including the use of JSON-RPC • TDD, which is a useful process for iterative software development (my opinion) TDD + MCP
  15. CONCLUSION What we saw: • MCP and MCP Tools as

    example requirements including the use of JSON-RPC • TDD, which is a useful process for iterative software development (my opinion) • An example MCP tool developed by TDD being used by Claude Sonnet 3.7 TDD + MCP
  16. REFERENCES • [1] JSON-RPC 2.0 https://www.jsonrpc.org/specification • [2] MCP Lifecycle

    (Initialization): https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/ • [3] MCP Tools: https://spec.modelcontextprotocol.io/specification/2024-11- 05/server/tools/ • [4] Claude for Desktop: https://support.anthropic.com/en/articles/10949351-getting- started-with-model-context-protocol-mcp-on-claude-for-desktop • [5] Larman, C. (2005). Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development. (3rd ed. pp. 385-394). Wait, J.