Skip to content

PoC setting up a MCP for trip planning #2611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ config :sentry,
root_source_code_paths: [File.cwd!()],
context_lines: 5

config :vancouver,
name: "MBTA MCP",
version: "1.0.0"

import_config "#{config_env()}.exs"
104 changes: 104 additions & 0 deletions lib/dotcom/tools/plan_trip.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule Dotcom.Tools.PlanTrip do
use Vancouver.Tool

@otp_module Application.compile_env!(:dotcom, :otp_module)
@params %OpenTripPlannerClient.PlanParams{
fromPlace: "South Station::42.352271,-71.055242",
toPlace: "North Station::42.365577,-71.06129",
date: "2025-06-25",
time: "12:00pm",
arriveBy: false,
numItineraries: 100,
transportModes: [
# %{mode: :TRAM},
%{mode: :SUBWAY},
# %{mode: :BUS},
# %{mode: :FERRY},
# %{mode: :RAIL}
],
wheelchair: false
}

def name, do: "plan_trip"
def description, do: "Plan a trip in Boston from A to B"

def input_schema do
%{
"type" => "object",
"properties" => %{
"from" => %{"type" => "string"},
"to" => %{"type" => "string"}
},
"required" => ["from", "to"]
}
end

def run(conn, %{"from" => _, "to" => _}) do
{:ok, [%OpenTripPlannerClient.ItineraryGroup{itineraries: itineraries} | _]} = @otp_module.plan(@params)

summary_string = itineraries |> List.first() |> itinerary_to_summary_string()
link_string = link_string(@params)
itineraries_strings = itineraries_to_strings(itineraries) |> List.last()

send_text(conn, summary_string <> link_string <> itineraries_strings)
end

defp distance(feet) when feet < 528 do
"a very short distance"
end

defp distance(feet) do
feet / 5280
|> Float.round(1)
|> Float.to_string()
|> Kernel.<>(" miles")
end

defp duration(seconds) do
minutes = seconds |> round() |> div(60)

if minutes < 60 do
"#{minutes} minutes"
else
hours = div(minutes, 60)
remaining_minutes = rem(minutes, 60)
"#{hours} hours #{remaining_minutes} minutes"
end
end

defp itineraries_to_strings(itineraries) when is_binary(itineraries) do
[itineraries]
end

defp itineraries_to_strings(itineraries) when is_list(itineraries) do
Enum.map(itineraries, &itinerary_to_string/1)
end

defp itinerary_to_string(itinerary) do
Enum.map(itinerary.legs, &leg_to_string/1) |> Enum.join("\n")
end

defp itinerary_to_summary_string(itinerary) do
tag = itinerary.tag |> Atom.to_string() |> Recase.to_title()

"The most #{tag} route will take #{duration(itinerary.duration)}.\n"
end

defp leg_to_string(%{mode: :WALK} = leg) do
"Walk #{distance(leg.distance)} #{leg.from.name} from #{leg.to.name}"
end

defp leg_to_string(%{mode: :BUS} = leg) do
"Ride the #{leg.route.short_name} bus for #{duration(leg.duration)} from #{leg.from.name} to #{leg.to.name}"
end

defp leg_to_string(leg) do
"Take the #{leg.route.long_name} from #{leg.from.name} to #{leg.to.name} for approximately #{duration(leg.duration)}"
end

defp link_string(params) do
encoded = Dotcom.TripPlan.AntiCorruptionLayer.encode(params)

"Visit MBTA's [Trip Planner](https://www.mbta.com/trip-planner?plan=#{encoded}) for more details.\n"
end
end
2 changes: 2 additions & 0 deletions lib/dotcom_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ defmodule DotcomWeb.Router do
plug(DotcomWeb.Plugs.CacheControl, max_age: 86_400)
end

forward "/mcp", Vancouver.Router, tools: [Dotcom.Tools.PlanTrip]

scope "/", DotcomWeb do
# no pipe
get("/_health", HealthController, :index)
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ defmodule DotCom.Mixfile do
{:typed_ecto_schema, "0.4.1"},
{:unrooted_polytree, "0.1.1"},
{:uuid, "1.1.8"},
{:vancouver, "0.3.0"},
{:wallaby, "0.30.10", [runtime: false, only: [:dev, :test]]},
{:yaml_elixir, "2.11.0", only: [:dev]},
{:ymlr, "5.1.3", only: [:dev]}
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"},
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
"ex_doc": {:hex, :ex_doc, "0.38.2", "504d25eef296b4dec3b8e33e810bc8b5344d565998cd83914ffe1b8503737c02", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "732f2d972e42c116a70802f9898c51b54916e542cc50968ac6980512ec90f42b"},
"ex_json_schema": {:hex, :ex_json_schema, "0.11.1", "b593f92937a095f66054bb318681397dfe7304e7d2b6b1a7534ea3aa40024f8c", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "32d651a575a6ce2fd613f140b0fef8dd0acc7cf8e8bcd29a3a1be5c945700dd5"},
"ex_machina": {:hex, :ex_machina, "2.8.0", "a0e847b5712065055ec3255840e2c78ef9366634d62390839d4880483be38abe", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "79fe1a9c64c0c1c1fab6c4fa5d871682cb90de5885320c187d117004627a7729"},
"ex_unit_summary": {:hex, :ex_unit_summary, "0.1.0", "7b0352afc5e6a933c805df0a539b66b392ac12ba74d8b208db7d83f77cb57049", [:mix], [], "hexpm", "8c87d0deade3657102902251d2ec60b5b94560004ce0e2c2fa5b466232716bd6"},
"excoveralls": {:hex, :excoveralls, "0.18.5", "e229d0a65982613332ec30f07940038fe451a2e5b29bce2a5022165f0c9b157e", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "523fe8a15603f86d64852aab2abe8ddbd78e68579c8525ae765facc5eae01562"},
Expand Down Expand Up @@ -134,6 +135,7 @@
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.1", "a48703a25c170eedadca83b11e88985af08d35f37c6f664d6dcfb106a97782fc", [:rebar3], [], "hexpm", "b3a917854ce3ae233619744ad1e0102e05673136776fb2fa76234f3e03b23642"},
"unrooted_polytree": {:hex, :unrooted_polytree, "0.1.1", "95027b1619d707fcbbd8980708a50efd170782142dd3de5112e9332d4cc27fef", [:mix], [], "hexpm", "9c8143d2015526ae49c3642ca509802e4db129685a57a0ec413e66546fe0c251"},
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm", "c790593b4c3b601f5dc2378baae7efaf5b3d73c4c6456ba85759905be792f2ac"},
"vancouver": {:hex, :vancouver, "0.3.0", "2bf3920e97bf98a79440fbafe3a1cdb936aa45ca4b34ab393f98855d18ccbb09", [:mix], [{:ex_json_schema, "~> 0.11", [hex: :ex_json_schema, repo: "hexpm", optional: false]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "d050b9864b833528dc9c87c5d3d2f6a36c0a070130053e2a7e5fae79c618c4fe"},
"vector": {:hex, :vector, "1.1.0", "0789b5e00e9c551d8d5880acab9a8f44ed46690d083af397018bf0c7f30c1092", [:mix], [], "hexpm", "48b0a800ec88e55b12c689b09100e4c9ba41ea1befb459221c085a4e70040696"},
"wallaby": {:hex, :wallaby, "0.30.10", "574afb8796521252daf49a4cd76a1c389d53cae5897f2d4b5f55dfae159c8e50", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}, {:httpoison, "~> 0.12 or ~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_ecto, ">= 3.0.0", [hex: :phoenix_ecto, repo: "hexpm", optional: true]}, {:web_driver_client, "~> 0.2.0", [hex: :web_driver_client, repo: "hexpm", optional: false]}], "hexpm", "a8f89b92d8acce37a94b5dfae6075c2ef00cb3689d6333f5f36c04b381c077b2"},
"web_driver_client": {:hex, :web_driver_client, "0.2.0", "63b76cd9eb3b0716ec5467a0f8bead73d3d9612e63f7560d21357f03ad86e31a", [:mix], [{:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:tesla, "~> 1.3", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "83cc6092bc3e74926d1c8455f0ce927d5d1d36707b74d9a65e38c084aab0350f"},
Expand Down
Loading