Gen-AI Developer Classroom notes 15/Jul/2025

Messages

  • lanchain has different types of Messages
    • System Message: We use this set context.
    • Human Message: This is question which we ask
    • AI Message: This is generally reply from model
    • Tool Message: This is message which involves calling tools
  • Lets interact with llm using these messages Refer Here
    Preview

Tools

  • LLMS have started adding capability which is called as function calling or tool calling
  • Langchain tools and also Refer Here
  • A tool w.r.t to python is a simple python function and generally has a decorator @tool

Integrate basic mcp with llm using langchain

  • To integrate mcp with langchain frameworks, langchain has built an adapter called as langchain-mcp-adapter
  • Activate your virtual environment and add package
uv add langchain-mcp-adapters
  • The agent or langchain RAG which you already have can speak with mcp with the help of this adapter
    Preview

Lets create a simple mcp server in python to perform arthimetic

  • Client
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain_mcp_adapters.tools import load_mcp_tools
import asyncio
from langchain_core.messages import HumanMessage
from langchain.chat_models import init_chat_model


def get_llm(name, provider):
    return init_chat_model(model=name, model_provider=provider)


model_name = "gemini-2.5-flash"
model_provider = "google_vertexai"
gemini_llm = get_llm(model_name, model_provider)

server_params = StdioServerParameters(
    command="python",
    # Make sure to update to the full absolute path to your math_server.py file
    args=[r".\mcp_math.py"],
)


def main():
    async def sync_adapter():
        async with stdio_client(server_params) as (read, write):
            async with ClientSession(read, write) as session:
                # Initialize the connection
                await session.initialize()

                # Get tools
                tools = await load_mcp_tools(session)
                gemini_llm_with_mcp_tools = gemini_llm.bind_tools(tools)

                # Ask LLM
                response = await gemini_llm_with_mcp_tools.ainvoke(
                    [HumanMessage(
                        content="I have 10 bikes i sold one, How many bikes do i have left ?")]
                )
                print(response)

    asyncio.run(sync_adapter())


# Call your main function synchronously
main()

By continuous learner

enthusiastic technology learner

Leave a Reply

Discover more from Direct AI Powered By Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading